Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/watcher/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions pkg/watcher/executor_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading