From 5be4ee913fb11374e79383256fd0a0fe6e710f38 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 10 Feb 2020 15:41:58 -0500 Subject: [PATCH] stack probe for test-kitchen Signed-off-by: Clinton Wolfe --- lib/inspec/utils/telemetry/run_context.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/inspec/utils/telemetry/run_context.rb b/lib/inspec/utils/telemetry/run_context.rb index ae97d68b4..27612dfcf 100644 --- a/lib/inspec/utils/telemetry/run_context.rb +++ b/lib/inspec/utils/telemetry/run_context.rb @@ -1,21 +1,27 @@ module Inspec module Telemetry # Guesses the run context of InSpec - how were we invoked? + # All stack values here are determined experimentally + class RunContextProbe def self.guess_run_context stack = caller_locations(4) + return "test-kitchen" if kitchen?(stack) return "cli" if run_by_thor?(stack) # audit-cookbook - # kitchen-inspec - # "unknown" + "unknown" end - def self.run_by_thor?(stack) stack_match(stack: stack[-14..-9], path: "thor/command", label: "run") && stack_match(stack: stack[-14..-9], path: "thor/invocation", label: "invoke_command") end + def self.kitchen?(stack) + stack_match(stack: stack[-21..-11], path: "kitchen/instance", label: "verify_action") && + stack_match(stack: stack[-14..-4], path: "kitchen/instance", label: "verify") + end + def self.stack_match(stack: nil, label: nil, path: nil) stack.any? do |frame| if label && path