* feat(go): make go fmt git-aware by default - By default, only check changed Go files (modified, staged, untracked) - Add --all flag to check all files (previous behaviour) - Reduces noise when running fmt on large codebases Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(build): minimal output by default, add missing i18n - Default output now shows single line: "Success Built N artifacts (dir)" - Add --verbose/-v flag to show full detailed output - Add all missing i18n translations for build commands - Errors still show failure reason in minimal mode Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat: add root-level `core git` command - Create pkg/gitcmd with git workflow commands as root menu - Export command builders from pkg/dev (AddCommitCommand, etc.) - Commands available under both `core git` and `core dev` for compatibility - Git commands: health, commit, push, pull, work, sync, apply - GitHub orchestration stays in dev: issues, reviews, ci, impact Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(qa): add docblock coverage checking Implement docblock/docstring coverage analysis for Go code: - New `core qa docblock` command to check coverage - Shows compact file:line list when under threshold - Integrate with `core go qa` as a default check - Add --docblock-threshold flag (default 80%) The checker uses Go AST parsing to find exported symbols (functions, types, consts, vars) without documentation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: address CodeRabbit review feedback - Fix doc comment: "status" → "health" in gitcmd package - Implement --check flag for `core go fmt` (exits non-zero if files need formatting) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs: add docstrings for 100% coverage Add documentation comments to all exported symbols: - pkg/build: ProjectType constants - pkg/cli: LogLevel, RenderStyle, TableStyle - pkg/framework: ServiceFor, MustServiceFor, Core.Core - pkg/git: GitError.Error, GitError.Unwrap - pkg/i18n: Handler Match/Handle methods - pkg/log: Level constants - pkg/mcp: Tool input/output types - pkg/php: Service constants, QA types, service methods - pkg/process: ServiceError.Error - pkg/repos: RepoType constants - pkg/setup: ChangeType, ChangeCategory constants - pkg/workspace: AddWorkspaceCommands Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore: standardize line endings to LF Add .gitattributes to enforce LF line endings for all text files. Normalize all existing files to use Unix-style line endings. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: address CodeRabbit review feedback - cmd_format.go: validate --check/--fix mutual exclusivity, capture stderr - cmd_docblock.go: return error instead of os.Exit(1) for proper error handling Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: address CodeRabbit review feedback (round 2) - linuxkit.go: propagate state update errors, handle cmd.Wait() errors in waitForExit - mcp.go: guard against empty old_string in editDiff to prevent runaway edits - cmd_docblock.go: log parse errors instead of silently skipping Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
257 lines
5.8 KiB
Go
257 lines
5.8 KiB
Go
package process
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/host-uk/core/pkg/framework"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestService(t *testing.T) (*Service, *framework.Core) {
|
|
t.Helper()
|
|
|
|
core, err := framework.New(
|
|
framework.WithName("process", NewService(Options{BufferSize: 1024})),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
svc, err := framework.ServiceFor[*Service](core, "process")
|
|
require.NoError(t, err)
|
|
|
|
return svc, core
|
|
}
|
|
|
|
func TestService_Start(t *testing.T) {
|
|
t.Run("echo command", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc, err := svc.Start(context.Background(), "echo", "hello")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, proc)
|
|
|
|
assert.NotEmpty(t, proc.ID)
|
|
assert.Equal(t, "echo", proc.Command)
|
|
assert.Equal(t, []string{"hello"}, proc.Args)
|
|
|
|
// Wait for completion
|
|
<-proc.Done()
|
|
|
|
assert.Equal(t, StatusExited, proc.Status)
|
|
assert.Equal(t, 0, proc.ExitCode)
|
|
assert.Contains(t, proc.Output(), "hello")
|
|
})
|
|
|
|
t.Run("failing command", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc, err := svc.Start(context.Background(), "sh", "-c", "exit 42")
|
|
require.NoError(t, err)
|
|
|
|
<-proc.Done()
|
|
|
|
assert.Equal(t, StatusExited, proc.Status)
|
|
assert.Equal(t, 42, proc.ExitCode)
|
|
})
|
|
|
|
t.Run("non-existent command", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
_, err := svc.Start(context.Background(), "nonexistent_command_xyz")
|
|
assert.Error(t, err)
|
|
})
|
|
|
|
t.Run("with working directory", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
|
Command: "pwd",
|
|
Dir: "/tmp",
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
<-proc.Done()
|
|
|
|
// On macOS /tmp is a symlink to /private/tmp
|
|
output := strings.TrimSpace(proc.Output())
|
|
assert.True(t, output == "/tmp" || output == "/private/tmp", "got: %s", output)
|
|
})
|
|
|
|
t.Run("context cancellation", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
proc, err := svc.Start(ctx, "sleep", "10")
|
|
require.NoError(t, err)
|
|
|
|
// Cancel immediately
|
|
cancel()
|
|
|
|
select {
|
|
case <-proc.Done():
|
|
// Good - process was killed
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("process should have been killed")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestService_Run(t *testing.T) {
|
|
t.Run("returns output", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
output, err := svc.Run(context.Background(), "echo", "hello world")
|
|
require.NoError(t, err)
|
|
assert.Contains(t, output, "hello world")
|
|
})
|
|
|
|
t.Run("returns error on failure", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
_, err := svc.Run(context.Background(), "sh", "-c", "exit 1")
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "exited with code 1")
|
|
})
|
|
}
|
|
|
|
func TestService_Actions(t *testing.T) {
|
|
t.Run("broadcasts events", func(t *testing.T) {
|
|
core, err := framework.New(
|
|
framework.WithName("process", NewService(Options{})),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
var started []ActionProcessStarted
|
|
var outputs []ActionProcessOutput
|
|
var exited []ActionProcessExited
|
|
var mu sync.Mutex
|
|
|
|
core.RegisterAction(func(c *framework.Core, msg framework.Message) error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
switch m := msg.(type) {
|
|
case ActionProcessStarted:
|
|
started = append(started, m)
|
|
case ActionProcessOutput:
|
|
outputs = append(outputs, m)
|
|
case ActionProcessExited:
|
|
exited = append(exited, m)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
svc, _ := framework.ServiceFor[*Service](core, "process")
|
|
proc, err := svc.Start(context.Background(), "echo", "test")
|
|
require.NoError(t, err)
|
|
|
|
<-proc.Done()
|
|
|
|
// Give time for events to propagate
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
assert.Len(t, started, 1)
|
|
assert.Equal(t, "echo", started[0].Command)
|
|
assert.Equal(t, []string{"test"}, started[0].Args)
|
|
|
|
assert.NotEmpty(t, outputs)
|
|
foundTest := false
|
|
for _, o := range outputs {
|
|
if strings.Contains(o.Line, "test") {
|
|
foundTest = true
|
|
break
|
|
}
|
|
}
|
|
assert.True(t, foundTest, "should have output containing 'test'")
|
|
|
|
assert.Len(t, exited, 1)
|
|
assert.Equal(t, 0, exited[0].ExitCode)
|
|
})
|
|
}
|
|
|
|
func TestService_List(t *testing.T) {
|
|
t.Run("tracks processes", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc1, _ := svc.Start(context.Background(), "echo", "1")
|
|
proc2, _ := svc.Start(context.Background(), "echo", "2")
|
|
|
|
<-proc1.Done()
|
|
<-proc2.Done()
|
|
|
|
list := svc.List()
|
|
assert.Len(t, list, 2)
|
|
})
|
|
|
|
t.Run("get by id", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc, _ := svc.Start(context.Background(), "echo", "test")
|
|
<-proc.Done()
|
|
|
|
got, err := svc.Get(proc.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, proc.ID, got.ID)
|
|
})
|
|
|
|
t.Run("get not found", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
_, err := svc.Get("nonexistent")
|
|
assert.ErrorIs(t, err, ErrProcessNotFound)
|
|
})
|
|
}
|
|
|
|
func TestService_Remove(t *testing.T) {
|
|
t.Run("removes completed process", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc, _ := svc.Start(context.Background(), "echo", "test")
|
|
<-proc.Done()
|
|
|
|
err := svc.Remove(proc.ID)
|
|
require.NoError(t, err)
|
|
|
|
_, err = svc.Get(proc.ID)
|
|
assert.ErrorIs(t, err, ErrProcessNotFound)
|
|
})
|
|
|
|
t.Run("cannot remove running process", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
proc, _ := svc.Start(ctx, "sleep", "10")
|
|
|
|
err := svc.Remove(proc.ID)
|
|
assert.Error(t, err)
|
|
|
|
cancel()
|
|
<-proc.Done()
|
|
})
|
|
}
|
|
|
|
func TestService_Clear(t *testing.T) {
|
|
t.Run("clears completed processes", func(t *testing.T) {
|
|
svc, _ := newTestService(t)
|
|
|
|
proc1, _ := svc.Start(context.Background(), "echo", "1")
|
|
proc2, _ := svc.Start(context.Background(), "echo", "2")
|
|
|
|
<-proc1.Done()
|
|
<-proc2.Done()
|
|
|
|
assert.Len(t, svc.List(), 2)
|
|
|
|
svc.Clear()
|
|
|
|
assert.Len(t, svc.List(), 0)
|
|
})
|
|
}
|