More attempts to duplicate #154

This commit is contained in:
Neale Pickett 2020-11-02 15:40:43 -06:00
parent e7ffa05a44
commit 2244c0764a
6 changed files with 90 additions and 3 deletions

View File

@ -0,0 +1,49 @@
#! /bin/sh -e
fail () {
echo "ERROR: $*" 1>&2
exit 1
}
case $1:$2:$3 in
inventory::)
cat <<EOT
{
"Puzzles": [1, 2, 3,
4, 5]
}
EOT
;;
puzzle:1:)
cat <<EOT
{
"Answers": ["answer1.0"],
"Pre": {
"Authors": ["author1.0"],
"Body": "<h1>moo.</h1>"
}
}
EOT
;;
puzzle:*)
fail "No such puzzle: $2"
;;
file:1:moo.txt)
echo "Moo."
;;
file:*:*)
fail "No such file: $2"
;;
answer:1:answer1.0)
echo -n '{"Correct":true}'
;;
answer:1:*)
echo '{"Correct":false}'
;;
answer:*:*)
fail "Fail answer"
;;
*)
fail "What is $1" 1>&2
;;
esac

View File

@ -9,6 +9,7 @@ import (
"os/exec"
"path"
"strconv"
"strings"
"time"
"github.com/spf13/afero"
@ -133,7 +134,12 @@ func (c FsCommandCategory) run(command string, args ...string) ([]byte, error) {
cmdargs := append([]string{command}, args...)
cmd := exec.CommandContext(ctx, "./"+path.Base(c.command), cmdargs...)
cmd.Dir = path.Dir(c.command)
return cmd.Output()
out, err := cmd.Output()
if err, ok := err.(*exec.ExitError); ok {
stderr := strings.TrimSpace(string(err.Stderr))
return nil, fmt.Errorf("%s (%s)", stderr, err.String())
}
return out, err
}
// Inventory returns a list of point values for this category.

View File

@ -3,6 +3,8 @@ package transpile
import (
"bytes"
"io"
"os/exec"
"strings"
"testing"
"github.com/spf13/afero"
@ -105,6 +107,23 @@ func TestOsFsCategory(t *testing.T) {
t.Error("File shouldn't exist")
}
if r, err := generated.Open(1, "cow.txt"); err != nil {
if e, ok := err.(*exec.ExitError); ok {
t.Error(err, string(e.Stderr))
} else {
t.Error(err)
}
} else {
defer r.Close()
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r); err != nil {
t.Error(err)
}
if !strings.Contains(buf.String(), "moo.") {
t.Errorf("Wrong body: %#v", buf.String())
}
}
if !generated.Answer(1, "answer1.0") {
t.Error("Correct answer failed")
}

View File

@ -383,7 +383,12 @@ func (fp FsCommandPuzzle) run(command string, args ...string) ([]byte, error) {
cmdargs := append([]string{command}, args...)
cmd := exec.CommandContext(ctx, "./"+path.Base(fp.command), cmdargs...)
cmd.Dir = path.Dir(fp.command)
return cmd.Output()
out, err := cmd.Output()
if err, ok := err.(*exec.ExitError); ok {
stderr := strings.TrimSpace(string(err.Stderr))
return nil, fmt.Errorf("%s (%s)", stderr, err.String())
}
return out, err
}
// Puzzle returns a Puzzle struct for the current puzzle.

View File

@ -0,0 +1,8 @@
______
< moo. >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

View File

@ -32,7 +32,7 @@ EOT
echo "Moo."
;;
file:*:*)
fail "No such file: $2"
cat "$3" || exit 1
;;
answer:1:answer1.0)
echo -n '{"Correct":true}'