@@ -3,15 +3,18 @@ use std::time::{Duration, UNIX_EPOCH};
33use crate :: records:: _helpers:: ids:: content_hash;
44use crate :: records:: _helpers:: ids:: diagnostic_record_id;
55use crate :: records:: _helpers:: ids:: output_record_id;
6+ use crate :: records:: main:: command_mode:: command_mode;
67use crate :: records:: main:: diagnostic_input_from_json:: diagnostic_input_from_json;
78use crate :: records:: main:: format_timestamp:: format_timestamp;
9+ use crate :: records:: main:: invocation_origin:: invocation_origin;
810use crate :: records:: main:: output_row:: output_row;
911use crate :: records:: main:: parse_timestamp:: parse_timestamp;
1012use crate :: records:: models:: { OutputChunk , OutputStream , RecallValue } ;
1113use crate :: records:: tests:: helpers:: { envelope, event_row, present, row_columns, text} ;
1214use crate :: records:: tests:: test_types:: {
13- ContentHashTestCase , DiagnosticTestCase , ParseTimestampTestCase , RecordIdTestCase ,
14- RejectedInputTestCase , RowColumnsTestCase , TimestampTestCase ,
15+ CommandModeTestCase , ContentHashTestCase , DiagnosticTestCase , OriginTestCase ,
16+ ParseTimestampTestCase , RecordIdTestCase , RejectedInputTestCase , RowColumnsTestCase ,
17+ TimestampTestCase ,
1518} ;
1619
1720#[ test]
@@ -274,3 +277,86 @@ fn given_incomplete_diagnostic_json_when_parsed_then_it_is_rejected() {
274277 ) ;
275278 }
276279}
280+
281+ #[ test]
282+ fn given_context_and_environment_when_classifying_then_the_origin_is_named ( ) {
283+ let test_cases = [
284+ OriginTestCase {
285+ description : "external context wins over CI" ,
286+ has_external_context : true ,
287+ environment : vec ! [ ( "CI" , "true" ) ] ,
288+ expected_origin : "integration" ,
289+ } ,
290+ OriginTestCase {
291+ description : "a CI system's variable marks CI" ,
292+ environment : vec ! [ ( "GITHUB_ACTIONS" , "true" ) ] ,
293+ has_external_context : false ,
294+ expected_origin : "ci" ,
295+ } ,
296+ OriginTestCase {
297+ description : "a URL value marks CI" ,
298+ environment : vec ! [ ( "JENKINS_URL" , "https://ci.example.com/" ) ] ,
299+ has_external_context : false ,
300+ expected_origin : "ci" ,
301+ } ,
302+ OriginTestCase {
303+ description : "false, 0 and empty values are not CI" ,
304+ environment : vec ! [ ( "CI" , "false" ) , ( "GITLAB_CI" , "0" ) , ( "BUILDKITE" , " " ) ] ,
305+ has_external_context : false ,
306+ expected_origin : "local" ,
307+ } ,
308+ OriginTestCase {
309+ description : "no variables is local" ,
310+ environment : Vec :: new ( ) ,
311+ has_external_context : false ,
312+ expected_origin : "local" ,
313+ } ,
314+ ] ;
315+ for test_case in test_cases {
316+ let environment = |name : & str | {
317+ test_case
318+ . environment
319+ . iter ( )
320+ . find ( |( key, _) | * key == name)
321+ . map ( |( _, value) | ( * value) . to_owned ( ) )
322+ } ;
323+ assert_eq ! (
324+ invocation_origin( test_case. has_external_context, environment) ,
325+ test_case. expected_origin,
326+ "{}" ,
327+ test_case. description
328+ ) ;
329+ }
330+ }
331+
332+ #[ test]
333+ fn given_a_command_and_its_flags_when_naming_its_mode_then_only_debug_has_one ( ) {
334+ let test_cases = [
335+ CommandModeTestCase {
336+ description : "debug without a connection checks configuration only" ,
337+ command : Some ( "debug" ) ,
338+ no_connection : true ,
339+ expected_mode : Some ( "config_only" ) ,
340+ } ,
341+ CommandModeTestCase {
342+ description : "debug also tests the connection" ,
343+ command : Some ( "debug" ) ,
344+ no_connection : false ,
345+ expected_mode : Some ( "config_and_connection" ) ,
346+ } ,
347+ CommandModeTestCase {
348+ description : "other commands record no mode" ,
349+ command : Some ( "build" ) ,
350+ no_connection : false ,
351+ expected_mode : None ,
352+ } ,
353+ ] ;
354+ for test_case in test_cases {
355+ assert_eq ! (
356+ command_mode( test_case. command, test_case. no_connection) ,
357+ test_case. expected_mode,
358+ "{}" ,
359+ test_case. description
360+ ) ;
361+ }
362+ }
0 commit comments