Skip to content
Merged
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
12 changes: 12 additions & 0 deletions internal/project/command_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !windows

package project

import (
"context"
"os/exec"
)

func newProjectCommandContext(ctx context.Context, name string, args ...string) *exec.Cmd {
return exec.CommandContext(ctx, name, args...)
}
20 changes: 20 additions & 0 deletions internal/project/command_other_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build !windows

package project

import (
"context"
"reflect"
"testing"
)

func TestNewProjectCommandContextPreservesDefaultProcessAttributes(t *testing.T) {
cmd := newProjectCommandContext(context.Background(), "git", "status")

if want := []string{"git", "status"}; !reflect.DeepEqual(cmd.Args, want) {
t.Fatalf("command arguments = %q; want %q", cmd.Args, want)
}
if cmd.SysProcAttr != nil {
t.Fatalf("SysProcAttr = %#v; want nil", cmd.SysProcAttr)
}
}
61 changes: 61 additions & 0 deletions internal/project/command_usage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package project

import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"runtime"
"testing"
)

func TestProjectDetectionGitCommandsUsePlatformHelper(t *testing.T) {
_, testFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("locate test source")
}

file, err := parser.ParseFile(token.NewFileSet(), filepath.Join(filepath.Dir(testFile), "detect.go"), nil, 0)
if err != nil {
t.Fatalf("parse detect.go: %v", err)
}

tests := []struct {
name string
}{
{name: "detectGitRootDir"},
{name: "detectFromGitRemote"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var function *ast.FuncDecl
for _, declaration := range file.Decls {
candidate, ok := declaration.(*ast.FuncDecl)
if ok && candidate.Name.Name == tt.name {
function = candidate
break
}
}
if function == nil {
t.Fatalf("function %s not found", tt.name)
}

helperCalls := 0
ast.Inspect(function.Body, func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
identifier, ok := call.Fun.(*ast.Ident)
if ok && identifier.Name == "newProjectCommandContext" {
helperCalls++
}
return true
})
if helperCalls != 1 {
t.Fatalf("%s uses newProjectCommandContext %d times; want 1", tt.name, helperCalls)
}
})
}
}
15 changes: 15 additions & 0 deletions internal/project/command_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build windows

package project

import (
"context"
"os/exec"
"syscall"
)

func newProjectCommandContext(ctx context.Context, name string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd
}
23 changes: 23 additions & 0 deletions internal/project/command_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build windows

package project

import (
"context"
"reflect"
"testing"
)

func TestNewProjectCommandContextHidesWindow(t *testing.T) {
cmd := newProjectCommandContext(context.Background(), "git", "status")

if want := []string{"git", "status"}; !reflect.DeepEqual(cmd.Args, want) {
t.Fatalf("command arguments = %q; want %q", cmd.Args, want)
}
if cmd.SysProcAttr == nil {
t.Fatal("SysProcAttr is nil; want Windows process attributes")
}
if !cmd.SysProcAttr.HideWindow {
t.Fatal("SysProcAttr.HideWindow is false; want true")
}
}
5 changes: 2 additions & 3 deletions internal/project/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -279,7 +278,7 @@ func detectGitRootDir(dir string) string {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "git", "-C", dir, "rev-parse", "--show-toplevel")
cmd := newProjectCommandContext(ctx, "git", "-C", dir, "rev-parse", "--show-toplevel")
out, err := cmd.Output()
if err != nil {
return ""
Expand Down Expand Up @@ -378,7 +377,7 @@ func detectFromGitRemote(dir string) string {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "git", "-C", dir, "remote", "get-url", "origin")
cmd := newProjectCommandContext(ctx, "git", "-C", dir, "remote", "get-url", "origin")
out, err := cmd.Output()
if err != nil {
return ""
Expand Down
1 change: 1 addition & 0 deletions plugin/pi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ function spawnDetached(command: string, args: readonly string[], cwd?: string):
try {
proc = spawn(command, [...args], {
cwd,
windowsHide: true,
detached: true,
stdio: "ignore",
});
Expand Down