chore(test): fix concurrency problem
This commit is contained in:
@@ -13,8 +13,7 @@ func TestBuildCommand(t *testing.T) {
|
|||||||
rootCmd := buildRootCmd()
|
rootCmd := buildRootCmd()
|
||||||
if rootCmd == nil {
|
if rootCmd == nil {
|
||||||
t.Errorf("Expected rootCmd to be defined")
|
t.Errorf("Expected rootCmd to be defined")
|
||||||
}
|
} else if rootCmd.Use != "katenary" {
|
||||||
if rootCmd.Use != "katenary" {
|
|
||||||
t.Errorf("Expected rootCmd.Use to be katenary, got %s", rootCmd.Use)
|
t.Errorf("Expected rootCmd.Use to be katenary, got %s", rootCmd.Use)
|
||||||
}
|
}
|
||||||
numCommands := 6
|
numCommands := 6
|
||||||
@@ -53,18 +52,27 @@ func TestSchemaCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
schema := generateSchemaCommand()
|
schema := generateSchemaCommand()
|
||||||
old := os.Stdout
|
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
|
os.Stdout = w
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
schema.Run(cmd, nil)
|
schema.Run(cmd, nil)
|
||||||
w.Close()
|
w.Close()
|
||||||
os.Stdout = old
|
close(done)
|
||||||
var buf bytes.Buffer
|
}()
|
||||||
io.Copy(&buf, r)
|
io.Copy(&buf, r)
|
||||||
output := buf.String()
|
<-done
|
||||||
|
|
||||||
|
os.Stdout = old
|
||||||
|
|
||||||
// try to parse json
|
// try to parse json
|
||||||
schemaContent := make(map[string]interface{})
|
schemaContent := make(map[string]any)
|
||||||
if err := json.Unmarshal([]byte(output), &schemaContent); err != nil {
|
if err := json.Unmarshal(buf.Bytes(), &schemaContent); err != nil {
|
||||||
t.Errorf("Expected valid json, got %s", output)
|
t.Errorf("Expected valid json")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user