diff --git a/pkg/watcher/executor.go b/pkg/watcher/executor.go index ac7362b..016b04a 100644 --- a/pkg/watcher/executor.go +++ b/pkg/watcher/executor.go @@ -33,6 +33,7 @@ func TriggerImport(entry config.WatchEntry) { mc, err = connectors.NewClient(*globalClientOpts) if err != nil { fmt.Printf("[ERROR] Cannot connect to Microcks client: %v in context '%s'\n", err, context) + continue } } else { // We have no config file, so just create a client with context as server URL. diff --git a/pkg/watcher/executor_test.go b/pkg/watcher/executor_test.go new file mode 100644 index 0000000..16082c6 --- /dev/null +++ b/pkg/watcher/executor_test.go @@ -0,0 +1,37 @@ +package watcher + +import ( + "os" + "path" + "testing" + + "github.com/microcks/microcks-cli/pkg/config" +) + +// TestTriggerImportSkipsContextWhenClientFails ensures TriggerImport does not +// panic when the Microcks client cannot be created for a context (for example +// an unknown or unresolvable context). Before the fix it dereferenced a nil +// client and crashed the watcher process. +func TestTriggerImportSkipsContextWhenClientFails(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + + // Write an empty local config so that resolving any context fails and + // NewClient returns a nil client with an error. + cfgDir := path.Join(home, ".config", "microcks") + if err := os.MkdirAll(cfgDir, 0o755); err != nil { + t.Fatalf("failed to create config dir: %v", err) + } + if err := os.WriteFile(path.Join(cfgDir, "config"), []byte("{}"), 0o600); err != nil { + t.Fatalf("failed to write config: %v", err) + } + + entry := config.WatchEntry{ + FilePath: path.Join(home, "spec.json"), + Context: []string{"missing-context"}, + MainArtifact: true, + } + + // Should return cleanly instead of panicking on a nil client. + TriggerImport(entry) +}