diff --git a/go/tests/cat_test/cat_correct/main.go b/go/tests/cat_test/cat_correct/main.go index 04d0a21a..6c64a22c 100644 --- a/go/tests/cat_test/cat_correct/main.go +++ b/go/tests/cat_test/cat_correct/main.go @@ -4,6 +4,8 @@ import ( "io" "io/ioutil" "os" + + "lib" ) func main() { @@ -13,11 +15,11 @@ func main() { } } else { for _, arg := range os.Args[1:] { - data, err := ioutil.ReadFile(arg) + b, err := ioutil.ReadFile(arg) if err != nil { - panic(err) + lib.Fatalln("ERROR:", err) } - os.Stdout.Write(data) + os.Stdout.Write(b) } } } diff --git a/go/tests/cat_test/main.go b/go/tests/cat_test/main.go index a99f6efc..929ce624 100644 --- a/go/tests/cat_test/main.go +++ b/go/tests/cat_test/main.go @@ -3,74 +3,25 @@ package main import ( "io/ioutil" "os" - "os/exec" "strings" - "github.com/01-edu/public/go/lib" + "lib" ) func main() { - pathFileName1 := "./student/cat/quest8.txt" - pathFileName2 := "./student/cat/quest8T.txt" - - if _, err := os.Stat(pathFileName1); err != nil { - lib.Fatalln(err) + file1 := "quest8.txt" + file2 := "quest8T.txt" + if err := ioutil.WriteFile(file1, []byte(lib.RandWords()+"\n"), os.ModePerm); err != nil { + panic(err) } - if _, err := os.Stat(pathFileName2); err != nil { - lib.Fatalln(err) + if err := ioutil.WriteFile(file2, []byte(lib.RandWords()+"\n"), os.ModePerm); err != nil { + panic(err) } - table := []string{pathFileName1, pathFileName1 + " " + pathFileName2, "asd"} + + table := []string{file1, file1 + " " + file2, "asd", "", file1 + " abc", "abc " + file2} for _, s := range table { lib.ChallengeMain("cat", strings.Fields(s)...) } - if _, err := exec.Command("go", "build", "-o", "cat_student", "./student/cat/main.go").Output(); err != nil { - lib.Fatal(string(err.(*exec.ExitError).Stderr)) - } - if _, err := exec.Command("go", "build", "-o", "cat_solution", "./solutions/cat/main.go").Output(); err != nil { - lib.Fatal(string(err.(*exec.ExitError).Stderr)) - } - pwd, err := os.Getwd() - if err != nil { - lib.Fatalln(err) - } - - for i := 0; i < 2; i++ { - randStdin := lib.RandAlnum() - cmd := exec.Command("sh", "-c", pwd+"/cat_solution") - solutionResult := execStdin(cmd, randStdin) - cmdS := exec.Command(pwd + "/cat_student") - studentResult := execStdin(cmdS, randStdin) - - if solutionResult != studentResult { - lib.Fatalf("./cat prints %s instead of %s\n", studentResult, solutionResult) - } - } + lib.ChallengeMainStdin("cat", lib.RandWords()+"\n") } - -func execStdin(cmd *exec.Cmd, randomStdin string) string { - stdin, err := cmd.StdinPipe() - if err != nil { - lib.Fatalln(err) - } - stdout, err := cmd.StdoutPipe() - if err != nil { - lib.Fatalln(err) - } - if err := cmd.Start(); err != nil { - lib.Fatalln(err) - } - _, err = stdin.Write([]byte(randomStdin)) - if err != nil { - lib.Fatalln(err) - } - stdin.Close() - - out, _ := ioutil.ReadAll(stdout) - if err := cmd.Wait(); err != nil { - lib.Fatalln(err) - } - return string(out) -} - -// TODO: handle stdin in ChallengeMain diff --git a/subjects/cat.en.md b/subjects/cat.en.md index 6b7d05bd..951eaa48 100644 --- a/subjects/cat.en.md +++ b/subjects/cat.en.md @@ -6,7 +6,7 @@ Write a program that behaves like a simplified `cat` command. - The options do not have to be handled. -- If the program is called without arguments it should take the standard input (stdin) and print it back (as shown with the "Hello" example below). +- If the program is called without arguments it should take the standard input (stdin) and print it back on the standard output (stdout). ```console student@ubuntu:~/[[ROOT]]/cat$ echo '"Programming is a skill best acquired by practice and example rather than from books" by Alan Turing' > quest8.txt @@ -18,6 +18,9 @@ student@ubuntu:~/[[ROOT]]/cat$ ./cat abc ERROR: abc: No such file or directory student@ubuntu:~/[[ROOT]]/cat$ ./cat quest8.txt "Programming is a skill best acquired by pratice and example rather than from books" by Alan Turing +student@ubuntu:~/[[ROOT]]/cat$ ./cat quest8.txt abc +"Programming is a skill best acquired by pratice and example rather than from books" by Alan Turing +ERROR: abc: No such file or directory student@ubuntu:~/[[ROOT]]/cat$ cat quest8.txt | ./cat "Programming is a skill best acquired by pratice and example rather than from books" by Alan Turing student@ubuntu:~/[[ROOT]]/cat$ ./cat