@@ -56,10 +56,21 @@ def run_onboarding(info: nil, error: nil, live_process: true)
5656 Open3 . capture3 ( bash_path , "-c" , script , chdir : @sandbox )
5757 end
5858
59- def run_dcr_parser ( parser_status : 0 , parser_value : "true" , write_output : true )
59+ def run_dcr_parser (
60+ parser_status : 0 ,
61+ parser_value : "true" ,
62+ parser_error : "" ,
63+ write_output : true ,
64+ create_tmp_dir : true ,
65+ remove_status : 0 ,
66+ remove_output_after_write : false ,
67+ initial_dcr_value : ""
68+ )
6069 bin_dir = File . join ( @sandbox , "bin" )
6170 tmp_dir = File . join ( @sandbox , "tmp" )
62- FileUtils . mkdir_p ( [ bin_dir , tmp_dir ] )
71+ error_capture = File . join ( @sandbox , "dcr-error" )
72+ FileUtils . mkdir_p ( bin_dir )
73+ FileUtils . mkdir_p ( tmp_dir ) if create_tmp_dir
6374 parser = File . join ( bin_dir , "ruby" )
6475 File . write (
6576 parser ,
@@ -69,17 +80,33 @@ def run_dcr_parser(parser_status: 0, parser_value: "true", write_output: true)
6980 if [ "${WRITE_OUTPUT}" == "true" ]; then
7081 printf '%s\\ n' "${PARSER_VALUE}" > "$output"
7182 fi
83+ if [ "${REMOVE_OUTPUT_AFTER_WRITE}" == "true" ]; then
84+ rm -f -- "$output"
85+ fi
86+ printf '%s' "${PARSER_ERROR}" >&2
7287 exit "${PARSER_STATUS}"
7388 SH
7489 )
7590 FileUtils . chmod ( 0o755 , parser )
91+ remove_stub = if remove_status != 0
92+ <<~SH
93+ rm() {
94+ echo "mock removal failure" >&2
95+ return "${REMOVE_STATUS}"
96+ }
97+ SH
98+ else
99+ ""
100+ end
76101 script = <<~SH
77102 #{ function_source ( "cleanupDcrOutput" ) }
78103 #{ function_source ( "parseDcrConfig" ) }
104+ #{ remove_stub }
79105 DCR_OUTPUT_FILE=""
80- DCR_VALUE=""
106+ DCR_VALUE="${INITIAL_DCR_VALUE} "
81107 parseDcrConfig
82108 status=$?
109+ printf '%s' "$DCR_ERROR" > "#{ shell_path ( error_capture ) } "
83110 printf '%s\\ n' "$DCR_VALUE"
84111 printf '%s\\ n' "$DCR_OUTPUT_FILE"
85112 exit "$status"
@@ -90,13 +117,19 @@ def run_dcr_parser(parser_status: 0, parser_value: "true", write_output: true)
90117 "TMPDIR" => shell_path ( tmp_dir ) ,
91118 "PARSER_STATUS" => parser_status . to_s ,
92119 "PARSER_VALUE" => parser_value ,
120+ "PARSER_ERROR" => parser_error ,
93121 "WRITE_OUTPUT" => write_output . to_s ,
122+ "REMOVE_STATUS" => remove_status . to_s ,
123+ "REMOVE_OUTPUT_AFTER_WRITE" => remove_output_after_write . to_s ,
124+ "INITIAL_DCR_VALUE" => initial_dcr_value ,
94125 } ,
95126 bash_path ,
96127 "-c" ,
97128 script ,
98129 chdir : @sandbox
99- ) . then { |stdout , stderr , status | [ stdout . lines . map ( &:chomp ) , stderr , status , tmp_dir ] }
130+ ) . then do |stdout , stderr , status |
131+ [ stdout . lines . map ( &:chomp ) , stderr , status , tmp_dir , File . read ( error_capture ) ]
132+ end
100133 end
101134
102135 def run_set_global_env_var ( env_path )
@@ -238,6 +271,68 @@ def test_parse_dcr_config_propagates_parser_failure_and_removes_temporary_output
238271 assert_empty Dir . glob ( File . join ( tmp_dir , "dcr_env_var.*" ) )
239272 end
240273
274+ def test_parse_dcr_config_preserves_parser_diagnostic
275+ _ , _ , status , _ , diagnostic = run_dcr_parser (
276+ parser_status : 23 ,
277+ parser_error : "Failed to write DCR parser output file"
278+ )
279+
280+ refute status . success?
281+ assert_equal "Failed to write DCR parser output file" , diagnostic
282+ end
283+
284+ def test_parse_dcr_config_removes_telemetry_error_marker_from_startup_diagnostic
285+ _ , _ , status , _ , diagnostic = run_dcr_parser (
286+ parser_status : 23 ,
287+ parser_error : "config::error::Failed to write DCR parser output file"
288+ )
289+
290+ refute status . success?
291+ assert_includes diagnostic , "Failed to write DCR parser output file"
292+ refute_includes diagnostic , "config::error::"
293+ end
294+
295+ def test_parse_dcr_config_clears_stale_value_before_failure
296+ output , _ , status , = run_dcr_parser (
297+ parser_status : 23 ,
298+ initial_dcr_value : "true"
299+ )
300+
301+ refute status . success?
302+ assert_equal "" , output . first
303+ end
304+
305+ def test_parse_dcr_config_captures_temporary_file_creation_error
306+ _ , stderr , status , _ , diagnostic = run_dcr_parser ( create_tmp_dir : false )
307+
308+ refute status . success?
309+ assert_empty stderr
310+ assert_includes diagnostic , "Failed to create DCR parser output file"
311+ assert_includes diagnostic , "dcr_env_var.XXXXXX"
312+ assert_includes diagnostic , "No such file or directory"
313+ end
314+
315+ def test_parse_dcr_config_captures_temporary_file_cleanup_error
316+ _ , stderr , status , _ , diagnostic = run_dcr_parser ( remove_status : 17 )
317+
318+ refute status . success? , "stderr=#{ stderr . inspect } diagnostic=#{ diagnostic . inspect } "
319+ assert_empty stderr
320+ assert_includes diagnostic , "Failed to remove DCR parser output file"
321+ assert_includes diagnostic , "mock removal failure"
322+ end
323+
324+ def test_parse_dcr_config_captures_output_open_error_without_stderr
325+ _ , stderr , status , _ , diagnostic = run_dcr_parser ( remove_output_after_write : true )
326+
327+ refute status . success?
328+ assert_empty stderr
329+ assert_includes diagnostic , "empty or unreadable"
330+ end
331+
332+ def test_dcr_wait_does_not_redirect_the_function_to_dev_null
333+ refute_match ( /until parseDcrConfig\s *>\s *\/ dev\/ null/ , File . read ( MAIN_PATH ) )
334+ end
335+
241336 def test_set_global_env_var_fails_when_state_cannot_be_written
242337 _ , _ , status = run_set_global_env_var ( File . join ( @sandbox , "missing" , "env_vars" ) )
243338
0 commit comments