mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: Add support for sandbox edit command (#385)
* feat: Add support for sandbox edit command * docs: Update sandbox docs * fix(editor): address review — neutral failure wording, skip sh-script tests on Windows Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(test): avoid pnpm init in pm-e2e — devEngines.packageManager crashes subsequent pnpm add Same workaround and rationale as the PNPM job in pmg-e2e.yml. Latest pnpm (unpinned pnpm/action-setup) writes devEngines.packageManager with onFail:download on init, and the next add fails with "Cannot use 'in' operator to search for 'integrity' in undefined". Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
bafd3c0654
commit
695a1d739d
@@ -0,0 +1,110 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func writeScript(t *testing.T, dir, body string) string {
|
||||
t.Helper()
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("editor scripts require /bin/sh")
|
||||
}
|
||||
path := filepath.Join(dir, "editor.sh")
|
||||
require.NoError(t, os.WriteFile(path, []byte("#!/bin/sh\n"+body+"\n"), 0o755))
|
||||
return path
|
||||
}
|
||||
|
||||
func TestResolve_VisualWins(t *testing.T) {
|
||||
t.Setenv("VISUAL", "visual-editor")
|
||||
t.Setenv("EDITOR", "other-editor")
|
||||
|
||||
editor, err := Resolve()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "visual-editor", editor)
|
||||
}
|
||||
|
||||
func TestResolve_EditorFallback(t *testing.T) {
|
||||
t.Setenv("VISUAL", "")
|
||||
t.Setenv("EDITOR", "fallback-editor")
|
||||
|
||||
editor, err := Resolve()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "fallback-editor", editor)
|
||||
}
|
||||
|
||||
func TestResolve_TrimsWhitespace(t *testing.T) {
|
||||
t.Setenv("VISUAL", " vim ")
|
||||
t.Setenv("EDITOR", "")
|
||||
|
||||
editor, err := Resolve()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "vim", editor)
|
||||
}
|
||||
|
||||
func TestResolve_PlatformDefault(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("unix default only")
|
||||
}
|
||||
t.Setenv("VISUAL", "")
|
||||
t.Setenv("EDITOR", "")
|
||||
|
||||
editor, err := Resolve()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "vi", editor)
|
||||
}
|
||||
|
||||
func TestOpen_RunsEditorWithPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
target := filepath.Join(dir, "target.txt")
|
||||
require.NoError(t, os.WriteFile(target, []byte("before\n"), 0o644))
|
||||
|
||||
t.Setenv("VISUAL", "")
|
||||
t.Setenv("EDITOR", writeScript(t, dir, `echo edited >> "$1"`))
|
||||
|
||||
require.NoError(t, Open(target))
|
||||
|
||||
data, err := os.ReadFile(target)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "before\nedited\n", string(data))
|
||||
}
|
||||
|
||||
func TestOpen_QuotedMultiWordEditor(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
dir := filepath.Join(base, "dir with space")
|
||||
require.NoError(t, os.MkdirAll(dir, 0o755))
|
||||
out := filepath.Join(base, "argv.txt")
|
||||
script := writeScript(t, dir, `printf '%s\n' "$@" > `+"'"+out+"'")
|
||||
|
||||
t.Setenv("VISUAL", "'"+script+"' --wait")
|
||||
t.Setenv("EDITOR", "")
|
||||
|
||||
require.NoError(t, Open("/some/target"))
|
||||
|
||||
data, err := os.ReadFile(out)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "--wait\n/some/target\n", string(data))
|
||||
}
|
||||
|
||||
func TestOpen_EditorExitNonZero(t *testing.T) {
|
||||
t.Setenv("VISUAL", "")
|
||||
t.Setenv("EDITOR", writeScript(t, t.TempDir(), `exit 3`))
|
||||
|
||||
err := Open("/some/target")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "editor")
|
||||
}
|
||||
|
||||
func TestOpen_InvalidQuoting(t *testing.T) {
|
||||
t.Setenv("VISUAL", "'unterminated")
|
||||
t.Setenv("EDITOR", "")
|
||||
|
||||
err := Open("/some/target")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid editor command")
|
||||
}
|
||||
Reference in New Issue
Block a user