forked from gokcehan/lf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start to implement gokcehan#1784, set shell as template for CMD on Wi…
…ndows - make `tokenize` to honor quotes - create function `firstField` to get only the first word from string - SlellCommand in os_windows.go now handles new type of gOpts.shell for example: `set shell 'cmd /c "%c %a"'` - tests for cmd.exe
- Loading branch information
Showing
4 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
|
||
func TestFirst(t *testing.T) { | ||
tests := []struct { | ||
in string | ||
out string | ||
}{ | ||
{"cmd", "cmd"}, | ||
{"cmd /c", "cmd"}, | ||
{`"C:\Program Files\echo.bat" hello world`, `"C:\Program Files\echo.bat"`}, | ||
{`ec\ ho -c "sdsd" 1 2 3`, `ec\ ho`}, | ||
} | ||
|
||
for _, test := range tests { | ||
start, end := firstField(test.in) | ||
r := test.in[start:end] | ||
if !reflect.DeepEqual(r, test.out) { | ||
t.Errorf("at input '%#v' expected '%#v' but parsed '%#v'", test.in, test.out, r) | ||
} | ||
} | ||
} | ||
|
||
func setup(_ *testing.T) (string, func(_ *testing.T)) { | ||
tmpDir, err := os.MkdirTemp("", "lfTestShellCommand") | ||
if err != nil { | ||
panic(err) | ||
} | ||
wd, err := os.Getwd() | ||
_ = wd | ||
if err != nil { | ||
panic(err) | ||
} | ||
os.Chdir(tmpDir) | ||
|
||
// Return a function to teardown the test | ||
return tmpDir, func(t *testing.T) { | ||
os.Chdir(wd) | ||
os.RemoveAll(tmpDir) | ||
} | ||
} | ||
|
||
func TestShellCommand_Cmd(t *testing.T) { | ||
tmpDir, teardown := setup(t) | ||
defer teardown(t) | ||
|
||
shell := `cmd.exe /c "%c %a"` | ||
gOpts.shell = shell | ||
|
||
run := func(c string, a []string) { | ||
cmd := shellCommand( c, a) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
if err != nil { | ||
t.Errorf("failed to run command '%#v' with args '%#v' %v", c, a, err) | ||
} | ||
} | ||
|
||
tmpBat := filepath.Join(tmpDir, "Test Folder With Spaces", "echo.bat") | ||
os.MkdirAll(filepath.Dir(tmpBat), os.ModePerm) | ||
f, err := os.Create(tmpBat) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.WriteString(` | ||
ECHO script args: %0 %* | ||
`) | ||
if err != nil { | ||
panic(err) | ||
} | ||
run(fmt.Sprintf(`"%s"`, tmpBat), []string{`hello friend`}) | ||
|
||
run(fmt.Sprintf(`dir "%s\Test Folder With Spaces" | %%PAGER%%`, tmpDir), []string{}) | ||
|
||
c := `mkdir "foo bar"` | ||
run(c, []string{}) | ||
if stat, err := os.Stat("foo bar"); err != nil || !stat.IsDir() { | ||
t.Errorf("failed to run command '%#v' %v", c, err) | ||
} | ||
} |