diff --git a/cli/sync_start.go b/cli/sync_start.go index a95935d8ef..05a2701297 100644 --- a/cli/sync_start.go +++ b/cli/sync_start.go @@ -57,15 +57,19 @@ func (*RootCmd) syncStart(socketPath *string) *serpent.Command { } ready := statusResp.IsReady - var waitedFor []string - if !ready { - for _, dep := range statusResp.Dependencies { - if !dep.IsSatisfied { - waitedFor = append(waitedFor, string(dep.DependsOn)) - } + var allDependencies []string + var unsatisfiedDependencies []string + for _, dep := range statusResp.Dependencies { + allDependencies = append(allDependencies, string(dep.DependsOn)) + if !dep.IsSatisfied { + unsatisfiedDependencies = append(unsatisfiedDependencies, string(dep.DependsOn)) } - slices.Sort(waitedFor) - waitedForList := strings.Join(waitedFor, ", ") + } + slices.Sort(allDependencies) + slices.Sort(unsatisfiedDependencies) + + if !ready { + waitedForList := strings.Join(unsatisfiedDependencies, ", ") cliui.Infof(i.Stdout, "Unit %q is waiting for dependencies to be satisfied: [%s]", unitName, waitedForList) @@ -96,10 +100,13 @@ func (*RootCmd) syncStart(socketPath *string) *serpent.Command { return xerrors.Errorf("start unit failed: %w", err) } - if len(waitedFor) == 0 { - cliui.Info(i.Stdout, "Success") - } else { - cliui.Info(i.Stdout, fmt.Sprintf("Unit %q finished waiting for dependencies: [%s]", unitName, strings.Join(waitedFor, ", "))) + switch { + case len(allDependencies) == 0: + cliui.Info(i.Stdout, fmt.Sprintf("Unit %q started with no dependencies", unitName)) + case len(unsatisfiedDependencies) == 0: + cliui.Info(i.Stdout, fmt.Sprintf("Unit %q started immediately, dependencies already satisfied: [%s]", unitName, strings.Join(allDependencies, ", "))) + default: + cliui.Info(i.Stdout, fmt.Sprintf("Unit %q finished waiting for dependencies: [%s]", unitName, strings.Join(unsatisfiedDependencies, ", "))) } return nil diff --git a/cli/sync_test.go b/cli/sync_test.go index 68864c2630..32ddede990 100644 --- a/cli/sync_test.go +++ b/cli/sync_test.go @@ -158,6 +158,42 @@ func TestSyncCommands_Golden(t *testing.T) { clitest.TestGoldenFile(t, "TestSyncCommands_Golden/start_with_dependencies", outBuf.Bytes(), nil) }) + t.Run("start_with_satisfied_dependencies", func(t *testing.T) { + t.Parallel() + path, cleanup := setupSocketServer(t) + defer cleanup() + + ctx := testutil.Context(t, testutil.WaitShort) + + // Set up dependencies: test-unit depends on dep-unit and dep-unit-2. + client, err := agentsocket.NewClient(ctx, agentsocket.WithPath(path)) + require.NoError(t, err) + + err = client.SyncWant(ctx, "test-unit", "dep-unit") + require.NoError(t, err) + err = client.SyncWant(ctx, "test-unit", "dep-unit-2") + require.NoError(t, err) + err = client.SyncStart(ctx, "dep-unit") + require.NoError(t, err) + err = client.SyncComplete(ctx, "dep-unit") + require.NoError(t, err) + err = client.SyncStart(ctx, "dep-unit-2") + require.NoError(t, err) + err = client.SyncComplete(ctx, "dep-unit-2") + require.NoError(t, err) + client.Close() + + var outBuf bytes.Buffer + inv, _ := clitest.New(t, "exp", "sync", "start", "test-unit", "--socket-path", path) + inv.Stdout = &outBuf + inv.Stderr = &outBuf + + err = inv.WithContext(ctx).Run() + require.NoError(t, err) + + clitest.TestGoldenFile(t, "TestSyncCommands_Golden/start_with_satisfied_dependencies", outBuf.Bytes(), nil) + }) + t.Run("want", func(t *testing.T) { t.Parallel() path, cleanup := setupSocketServer(t) diff --git a/cli/testdata/TestSyncCommands_Golden/start_no_dependencies.golden b/cli/testdata/TestSyncCommands_Golden/start_no_dependencies.golden index 35821117c8..a48a7f51ce 100644 --- a/cli/testdata/TestSyncCommands_Golden/start_no_dependencies.golden +++ b/cli/testdata/TestSyncCommands_Golden/start_no_dependencies.golden @@ -1 +1 @@ -Success +Unit "test-unit" started with no dependencies diff --git a/cli/testdata/TestSyncCommands_Golden/start_with_satisfied_dependencies.golden b/cli/testdata/TestSyncCommands_Golden/start_with_satisfied_dependencies.golden new file mode 100644 index 0000000000..c71c1288f6 --- /dev/null +++ b/cli/testdata/TestSyncCommands_Golden/start_with_satisfied_dependencies.golden @@ -0,0 +1 @@ +Unit "test-unit" started immediately, dependencies already satisfied: [dep-unit, dep-unit-2]