-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
- Loading branch information
1 parent
bf75a2b
commit ba0255d
Showing
11 changed files
with
197 additions
and
1 deletion.
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,47 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
log.Printf("%+v", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run() error { | ||
flag.Parse() | ||
if len(flag.Args()) < 2 { | ||
return errors.New("at least 2 arguments required") | ||
} | ||
cmd := &exec.Cmd{ | ||
Args: flag.Args()[1:], | ||
} | ||
argv0 := flag.Arg(0) | ||
if filepath.IsAbs(argv0) { | ||
cmd.Path = argv0 | ||
} else if strings.HasPrefix(argv0, "./") { | ||
p, err := filepath.Abs(argv0) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Path = p | ||
} else { | ||
p, err := exec.LookPath(argv0) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Path = p | ||
} | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} |
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,11 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
fmt.Println(strings.Join(os.Args, " ")) | ||
} |
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,26 @@ | ||
#!/usr/bin/env sh | ||
|
||
go build print/printargs.go | ||
go build exec/execargv0.go | ||
|
||
echo "testing $(uname -m)" | ||
./test.bats | ||
|
||
crossArch=arm64 | ||
crossEmulator=aarch64 | ||
|
||
if [ "$(uname -m)" = "aarch64" ]; then | ||
crossArch="amd64" | ||
crossEmulator="x86_64" | ||
fi | ||
|
||
GOARCH=$crossArch go build print/printargs.go | ||
GOARCH=$crossArch go build exec/execargv0.go | ||
|
||
if ./printargs >/dev/null 2>/dev/nulll; then | ||
echo "can't test emulator because $crossEmulator emulator is installed in the kernel" | ||
exit 1 | ||
fi | ||
|
||
echo "testing $crossEmulator" | ||
BINFMT_EMULATOR=$crossEmulator ./test.bats |
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 @@ | ||
#!./printargs |
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 @@ | ||
#!./printargs arg |
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 @@ | ||
#!/work/printargs |
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 @@ | ||
#!/work/shebang3.sh |
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,87 @@ | ||
#!/usr/bin/env bats | ||
|
||
load "assert" | ||
|
||
exec0() { | ||
if [ -z "$BINFMT_EMULATOR" ]; then | ||
run ./execargv0 "$@" | ||
else | ||
run buildkit-qemu-$BINFMT_EMULATOR ./execargv0 "$@" | ||
fi | ||
} | ||
|
||
execdirect() { | ||
if [ -z "$BINFMT_EMULATOR" ]; then | ||
run "$@" | ||
else | ||
run buildkit-qemu-$BINFMT_EMULATOR "$@" | ||
fi | ||
} | ||
|
||
|
||
@test "exec-single" { | ||
exec0 ./printargs foo bar1 bar2 | ||
assert_success | ||
assert_output "foo bar1 bar2" | ||
} | ||
|
||
@test "exec-multi" { | ||
exec0 ./execargv0 ./printargs ./printargs baz | ||
assert_success | ||
assert_output "baz" | ||
} | ||
|
||
@test "exec-multi-abs" { | ||
exec0 ./execargv0 $(pwd)/printargs $(pwd)/printargs baz | ||
assert_success | ||
assert_output "baz" | ||
} | ||
|
||
@test "exec-multi-path" { | ||
cp $(pwd)/printargs /usr/bin/test-printargs | ||
exec0 test-printargs test-printargs abc | ||
assert_success | ||
assert_output "test-printargs abc" | ||
} | ||
|
||
@test "exec-direct" { | ||
execdirect test-printargs foo bar1 | ||
assert_success | ||
assert_output "test-printargs foo bar1" | ||
} | ||
|
||
@test "exec-direct-abs" { | ||
execdirect $(pwd)/printargs foo bar1 | ||
assert_success | ||
assert_output "$(pwd)/printargs foo bar1" | ||
} | ||
|
||
@test "shebang" { | ||
exec0 ./shebang.sh arg1 arg2 | ||
assert_success | ||
assert_output "./printargs $(pwd)/shebang.sh arg2" | ||
} | ||
|
||
@test "shebang-arg" { | ||
exec0 ./shebang2.sh arg1 arg2 | ||
assert_success | ||
assert_output "./printargs arg $(pwd)/shebang2.sh arg2" | ||
} | ||
|
||
@test "shebang-abs" { | ||
exec0 ./shebang3.sh arg1 arg2 | ||
assert_success | ||
assert_output "/work/printargs $(pwd)/shebang3.sh arg2" | ||
} | ||
|
||
@test "shebang-multi" { | ||
exec0 ./shebang4.sh arg1 arg2 | ||
assert_success | ||
assert_output "/work/printargs $(pwd)/shebang3.sh $(pwd)/shebang4.sh arg2" | ||
} | ||
|
||
@test "shebang-direct" { | ||
execdirect ./shebang.sh foo bar1 | ||
assert_success | ||
assert_output "./printargs ./shebang.sh foo bar1" | ||
} |