From 905b3bd932f4c0461cb544d2d41d29f8add28aa4 Mon Sep 17 00:00:00 2001 From: xpetit <32063953+xpetit@users.noreply.github.com> Date: Sun, 4 Apr 2021 13:52:14 +0200 Subject: [PATCH] Refactor the way Go exercises are tested to make it easy to test --- go/tests/README.md | 29 +++++++++++++++++++++++++++++ go/tests/entrypoint.sh | 9 ++++----- go/tests/lib/lib.go | 23 +++++++++++++++-------- 3 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 go/tests/README.md diff --git a/go/tests/README.md b/go/tests/README.md new file mode 100644 index 000000000..ca8e1e23a --- /dev/null +++ b/go/tests/README.md @@ -0,0 +1,29 @@ +# Tests + +To run the tests make sure the two repositories are right next to each other: + +- github.com/01-edu/piscine-go +- github.com/01-edu/public + +To test a program, run this command in this folder (`public/go/tests`): + +``` +go run github.com/01-edu/public/go/tests/func/EXERCISE_test +``` + +and a program: + +``` +go run github.com/01-edu/public/go/tests/prog/EXERCISE_test +``` + +Relative paths works anywhere within `public/go/tests`: + +``` +go run ./prog/printalphabet_test +cd prog +go run ./printalphabet_test +go run ../func/isnegative_test/ +``` + +No output means success. diff --git a/go/tests/entrypoint.sh b/go/tests/entrypoint.sh index 581d36790..e6dcfa500 100755 --- a/go/tests/entrypoint.sh +++ b/go/tests/entrypoint.sh @@ -2,7 +2,7 @@ set -e -cp -r /public/go/tests . +cp -r /public . cp -a student piscine-go cd piscine-go @@ -44,14 +44,13 @@ if test "$ALLOWED_FUNCTIONS" && test "$FILE"; then rc "$FILE" $ALLOWED_FUNCTIONS fi +cd ~/public/go/tests + # Compile and run test if command -v "${EXERCISE}_test" >/dev/null 2>&1; then # The exercise is a program - go build -o exe "./$EXERCISE" "${EXERCISE}_test" else # The exercise is a function - cd "$HOME/tests/func/${EXERCISE}_test" - go mod edit -replace "student=$HOME/piscine-go" - go run . + go run "./func/${EXERCISE}_test" fi diff --git a/go/tests/lib/lib.go b/go/tests/lib/lib.go index e35aae413..58d3e0920 100644 --- a/go/tests/lib/lib.go +++ b/go/tests/lib/lib.go @@ -8,6 +8,7 @@ import ( "math/rand" "os" "os/exec" + "path" "reflect" "strconv" "strings" @@ -282,9 +283,7 @@ type Output struct { func Monitor(fn interface{}, args []interface{}) (out Output) { old := os.Stdout r, w, err := os.Pipe() - if err != nil { - Fatalln("Cannot create pipe.") - } + panicIfNotNil(err) os.Stdout = w out.Results = Call(fn, args) outC := make(chan string) @@ -335,9 +334,17 @@ func Fatalf(format string, a ...interface{}) { os.Exit(1) } +func panicIfNotNil(err error) { + if err != nil { + panic(err) + } +} + func ChallengeMainStdin(exercise, input string, args ...string) { - run := func(name string) (string, int) { - cmd := exec.Command(name, args...) + run := func(pkg string) (string, int) { + binaryPath := path.Join(os.TempDir(), "binaries", path.Base(pkg)) + panicIfNotNil(exec.Command("go", "build", "-o", binaryPath, pkg).Run()) + cmd := exec.Command(binaryPath, args...) if input != "" { cmd.Stdin = bytes.NewBufferString(input) } @@ -346,7 +353,7 @@ func ChallengeMainStdin(exercise, input string, args ...string) { if ee, ok := err.(*exec.ExitError); ok { return string(b), ee.ExitCode() } - Fatalln(err) + panic(err) } return string(b), 0 } @@ -364,8 +371,8 @@ func ChallengeMainStdin(exercise, input string, args ...string) { code := func(code int) string { return fmt.Sprintf("echo $?\n%d\n$", code) } - student, studentCode := run("./exe") - solution, solutionCode := run(exercise + "_prog") + student, studentCode := run(path.Join("student", exercise)) + solution, solutionCode := run(path.Join("github.com/01-edu/public/go/tests/prog", exercise+"_prog")) if solutionCode == 0 { if studentCode != 0 { Fatalln("Your program fails (non-zero exit status) when it should not :\n" +