Browse Source

Refactor the way Go exercises are tested to make it easy to test

pull/768/head
xpetit 4 years ago
parent
commit
905b3bd932
No known key found for this signature in database
GPG Key ID: 97C60669182C17A5
  1. 29
      go/tests/README.md
  2. 9
      go/tests/entrypoint.sh
  3. 23
      go/tests/lib/lib.go

29
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.

9
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

23
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" +

Loading…
Cancel
Save