From e9e7c5f7b5ea7b42134fb04affd889b52ebd58ba Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Sun, 3 Aug 2025 14:12:49 +0200 Subject: [PATCH] chore(test): fix concurrency problem --- cmd/katenary/main_test.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/katenary/main_test.go b/cmd/katenary/main_test.go index 679fee7..758806b 100644 --- a/cmd/katenary/main_test.go +++ b/cmd/katenary/main_test.go @@ -13,8 +13,7 @@ func TestBuildCommand(t *testing.T) { rootCmd := buildRootCmd() if rootCmd == nil { t.Errorf("Expected rootCmd to be defined") - } - if rootCmd.Use != "katenary" { + } else if rootCmd.Use != "katenary" { t.Errorf("Expected rootCmd.Use to be katenary, got %s", rootCmd.Use) } numCommands := 6 @@ -53,18 +52,27 @@ func TestSchemaCommand(t *testing.T) { } schema := generateSchemaCommand() old := os.Stdout - r, w, _ := os.Pipe() + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("Failed to create pipe: %v", err) + } os.Stdout = w - schema.Run(cmd, nil) - w.Close() - os.Stdout = old var buf bytes.Buffer + + done := make(chan struct{}) + go func() { + schema.Run(cmd, nil) + w.Close() + close(done) + }() io.Copy(&buf, r) - output := buf.String() + <-done + + os.Stdout = old // try to parse json - schemaContent := make(map[string]interface{}) - if err := json.Unmarshal([]byte(output), &schemaContent); err != nil { - t.Errorf("Expected valid json, got %s", output) + schemaContent := make(map[string]any) + if err := json.Unmarshal(buf.Bytes(), &schemaContent); err != nil { + t.Errorf("Expected valid json") } }