Skip to content

Commit 7c960ac

Browse files
committed
fix(stop): extract resolveStopTarget and add tests
Extract context resolution logic into resolveStopTarget() function with explicit precedence comment (context name checked first). Add 5 targeted tests: resolve by context name, instance name, not-found, collision (context wins), and empty name fallback. Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
1 parent 7b4ece9 commit 7c960ac

2 files changed

Lines changed: 139 additions & 16 deletions

File tree

cmd/stop.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,11 @@ microcks stop --name myinstance`,
3535

3636
var ctx *config.Context
3737
if name != "" {
38-
ctx, err = localConfig.ResolveContext(name)
39-
if err != nil {
40-
var ctxRef *config.ContextRef
41-
for i := range localConfig.Contexts {
42-
if localConfig.Contexts[i].Instance == name {
43-
ctxRef = &localConfig.Contexts[i]
44-
break
45-
}
46-
}
47-
if ctxRef == nil {
48-
log.Fatalf("No context found for '%s'", name)
49-
}
50-
ctx, err = localConfig.ResolveContext(ctxRef.Name)
51-
errors.CheckError(err)
52-
}
38+
ctx, err = resolveStopTarget(name, localConfig)
5339
} else {
5440
ctx, err = localConfig.ResolveContext("")
55-
errors.CheckError(err)
5641
}
42+
errors.CheckError(err)
5743
instance := ctx.Instance
5844

5945
if instance.Name == "" {
@@ -102,3 +88,20 @@ microcks stop --name myinstance`,
10288

10389
return stopCmd
10490
}
91+
92+
// resolveStopTarget resolves a stop target by name.
93+
// Context name is checked first because it is the explicit user-assigned
94+
// identifier. Instance name is a secondary label (from start --name) —
95+
// used as fallback when no context with the given name exists.
96+
func resolveStopTarget(name string, cfg *config.LocalConfig) (*config.Context, error) {
97+
ctx, err := cfg.ResolveContext(name)
98+
if err == nil {
99+
return ctx, nil
100+
}
101+
for i := range cfg.Contexts {
102+
if cfg.Contexts[i].Instance == name {
103+
return cfg.ResolveContext(cfg.Contexts[i].Name)
104+
}
105+
}
106+
return nil, fmt.Errorf("no context found for '%s'", name)
107+
}

cmd/stop_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"testing"
21+
22+
"github.com/microcks/microcks-cli/pkg/config"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestResolveStopTargetByContextName(t *testing.T) {
28+
cfg := &config.LocalConfig{
29+
Contexts: []config.ContextRef{
30+
{Name: "dev", Server: "http://dev-server:8080", User: "u1"},
31+
},
32+
Servers: []config.Server{
33+
{Server: "http://dev-server:8080"},
34+
},
35+
Users: []config.User{
36+
{Name: "u1"},
37+
},
38+
}
39+
40+
ctx, err := resolveStopTarget("dev", cfg)
41+
require.NoError(t, err)
42+
assert.Equal(t, "dev", ctx.Name)
43+
}
44+
45+
func TestResolveStopTargetByInstanceName(t *testing.T) {
46+
cfg := &config.LocalConfig{
47+
Contexts: []config.ContextRef{
48+
{Name: "http://server", Server: "http://instance-server:8080", User: "u1", Instance: "myinst"},
49+
},
50+
Servers: []config.Server{
51+
{Server: "http://instance-server:8080"},
52+
},
53+
Users: []config.User{
54+
{Name: "u1"},
55+
},
56+
}
57+
58+
ctx, err := resolveStopTarget("myinst", cfg)
59+
require.NoError(t, err)
60+
assert.Equal(t, "http://server", ctx.Name)
61+
}
62+
63+
func TestResolveStopTargetNotFound(t *testing.T) {
64+
cfg := &config.LocalConfig{
65+
Contexts: []config.ContextRef{
66+
{Name: "dev", Server: "http://dev-server:8080", User: "u1"},
67+
},
68+
Servers: []config.Server{
69+
{Server: "http://dev-server:8080"},
70+
},
71+
Users: []config.User{
72+
{Name: "u1"},
73+
},
74+
}
75+
76+
_, err := resolveStopTarget("nonexistent", cfg)
77+
require.Error(t, err)
78+
assert.Contains(t, err.Error(), "nonexistent")
79+
}
80+
81+
func TestResolveStopTargetCollisionContextWins(t *testing.T) {
82+
cfg := &config.LocalConfig{
83+
Contexts: []config.ContextRef{
84+
{Name: "shared", Server: "http://server-a:8080", User: "u1", Instance: "other"},
85+
{Name: "different", Server: "http://server-b:8080", User: "u2", Instance: "shared"},
86+
},
87+
Servers: []config.Server{
88+
{Server: "http://server-a:8080"},
89+
{Server: "http://server-b:8080"},
90+
},
91+
Users: []config.User{
92+
{Name: "u1"},
93+
{Name: "u2"},
94+
},
95+
}
96+
97+
ctx, err := resolveStopTarget("shared", cfg)
98+
require.NoError(t, err)
99+
assert.Equal(t, "shared", ctx.Name)
100+
assert.Equal(t, "http://server-a:8080", ctx.Server.Server)
101+
}
102+
103+
func TestResolveStopTargetEmptyName(t *testing.T) {
104+
cfg := &config.LocalConfig{
105+
CurrentContext: "dev",
106+
Contexts: []config.ContextRef{
107+
{Name: "dev", Server: "http://dev-server:8080", User: "u1"},
108+
},
109+
Servers: []config.Server{
110+
{Server: "http://dev-server:8080"},
111+
},
112+
Users: []config.User{
113+
{Name: "u1"},
114+
},
115+
}
116+
117+
ctx, err := resolveStopTarget("", cfg)
118+
require.NoError(t, err)
119+
assert.Equal(t, "dev", ctx.Name)
120+
}

0 commit comments

Comments
 (0)