From 96af961a38ddf916992e6fe35470c47e5e73b6a5 Mon Sep 17 00:00:00 2001 From: Xavier Petit <32063953+xpetit@users.noreply.github.com> Date: Mon, 13 Apr 2020 13:11:00 +0200 Subject: [PATCH] Make printprogramname subject, test & solution more consistent and slightly more difficult --- subjects/printprogramname.en.md | 5 +++- tests/go/printprogramname_test.go | 29 ++++++------------- .../solutions/printprogramname/programname.go | 3 +- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/subjects/printprogramname.en.md b/subjects/printprogramname.en.md index 7cef297e9..8337b14c9 100644 --- a/subjects/printprogramname.en.md +++ b/subjects/printprogramname.en.md @@ -9,6 +9,9 @@ Example of output : ```console student@ubuntu:~/[[ROOT]]/printprogramname$ go build main.go student@ubuntu:~/[[ROOT]]/printprogramname$ ./main -./main +main +student@ubuntu:~/[[ROOT]]/printprogramname$ go build +student@ubuntu:~/[[ROOT]]/printprogramname$ ./printprogramname +printprogramname student@ubuntu:~/[[ROOT]]/printprogramname$ ``` diff --git a/tests/go/printprogramname_test.go b/tests/go/printprogramname_test.go index 28bfd4ad3..80c675049 100644 --- a/tests/go/printprogramname_test.go +++ b/tests/go/printprogramname_test.go @@ -1,30 +1,19 @@ package student_test import ( - "strings" + "bytes" + "os/exec" "testing" - - "github.com/01-edu/z01" ) func TestPrintProgramName(t *testing.T) { - exercise := strings.ToLower( - strings.TrimPrefix(t.Name(), "Test")) - out, err1 := z01.MainOut("./student/printprogramname") - if err1 != nil { - t.Fatalf(err1.Error()) - } - - correct, err2 := z01.MainOut("./solutions/printprogramname") - - if err2 != nil { - t.Fatalf(err2.Error()) + b, err := exec.Command("go", "run", "./student/printprogramname").Output() + if err != nil { + t.Fatal(err) } - - arrOut := strings.Split(out, "/") - ArrCor := strings.Split(correct, "/") - if ArrCor[len(ArrCor)-1] != arrOut[len(arrOut)-1] { - t.Fatalf("./%s prints %q instead of %q\n", - exercise, arrOut[len(arrOut)-1], ArrCor[len(ArrCor)-1]) + if string(bytes.TrimSpace(b)) != "printprogramname" { + t.Fatal("Failed to print the program name") } } + +// TODO: add more test cases (different program names), to do so compile then rename and test the binary several times diff --git a/tests/go/solutions/printprogramname/programname.go b/tests/go/solutions/printprogramname/programname.go index 50bbf255a..aab99c63d 100644 --- a/tests/go/solutions/printprogramname/programname.go +++ b/tests/go/solutions/printprogramname/programname.go @@ -3,8 +3,9 @@ package main import ( "fmt" "os" + "path/filepath" ) func main() { - fmt.Println(os.Args[0]) + fmt.Println(filepath.Base(os.Args[0])) }