Browse Source

Add terminal color detection to logging in `core:testing`

Feoramund 3 months ago
parent
commit
3c40a54dcd
1 changed files with 17 additions and 2 deletions
  1. 17 2
      core/testing/runner.odin

+ 17 - 2
core/testing/runner.odin

@@ -24,6 +24,7 @@ import "core:os"
 import "core:slice"
 @require import "core:strings"
 import "core:sync/chan"
+import "core:terminal"
 import "core:terminal/ansi"
 import "core:thread"
 import "core:time"
@@ -70,6 +71,8 @@ get_log_level :: #force_inline proc() -> runtime.Logger_Level {
 	}
 }
 
+@(private) global_log_colors_disabled: bool
+
 JSON :: struct {
 	total:    int,
 	success:  int,
@@ -129,11 +132,16 @@ run_test_task :: proc(task: thread.Task) {
 	
 	context.assertion_failure_proc = test_assertion_failure_proc
 
+	logger_options := Default_Test_Logger_Opts
+	if global_log_colors_disabled {
+		logger_options -= {.Terminal_Color}
+	}
+
 	context.logger = {
 		procedure = test_logger_proc,
 		data = &data.t,
 		lowest_level = get_log_level(),
-		options = Default_Test_Logger_Opts,
+		options = logger_options,
 	}
 
 	random_generator_state: runtime.Default_Random_State
@@ -211,6 +219,8 @@ runner :: proc(internal_tests: []Internal_Test) -> bool {
 	stdout := io.to_writer(os.stream_from_handle(os.stdout))
 	stderr := io.to_writer(os.stream_from_handle(os.stderr))
 
+	global_log_colors_disabled = !terminal.color_enabled || !terminal.is_terminal(os.stderr)
+
 	// -- Prepare test data.
 
 	alloc_error: mem.Allocator_Error
@@ -442,11 +452,16 @@ runner :: proc(internal_tests: []Internal_Test) -> bool {
 	// digging through the source to divine everywhere it is used for that.
 	shared_log_allocator := context.allocator
 
+	logger_options := Default_Test_Logger_Opts - {.Short_File_Path, .Line, .Procedure}
+	if global_log_colors_disabled {
+		logger_options -= {.Terminal_Color}
+	}
+
 	context.logger = {
 		procedure = runner_logger_proc,
 		data = &log_messages,
 		lowest_level = get_log_level(),
-		options = Default_Test_Logger_Opts - {.Short_File_Path, .Line, .Procedure},
+		options = logger_options,
 	}
 
 	run_index: int