mirror of https://github.com/dirtbags/moth.git
More attempts to duplicate #154
This commit is contained in:
parent
e7ffa05a44
commit
2244c0764a
|
@ -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
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
@ -133,7 +134,12 @@ func (c FsCommandCategory) run(command string, args ...string) ([]byte, error) {
|
||||||
cmdargs := append([]string{command}, args...)
|
cmdargs := append([]string{command}, args...)
|
||||||
cmd := exec.CommandContext(ctx, "./"+path.Base(c.command), cmdargs...)
|
cmd := exec.CommandContext(ctx, "./"+path.Base(c.command), cmdargs...)
|
||||||
cmd.Dir = path.Dir(c.command)
|
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.
|
// Inventory returns a list of point values for this category.
|
||||||
|
|
|
@ -3,6 +3,8 @@ package transpile
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
@ -105,6 +107,23 @@ func TestOsFsCategory(t *testing.T) {
|
||||||
t.Error("File shouldn't exist")
|
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") {
|
if !generated.Answer(1, "answer1.0") {
|
||||||
t.Error("Correct answer failed")
|
t.Error("Correct answer failed")
|
||||||
}
|
}
|
||||||
|
|
|
@ -383,7 +383,12 @@ func (fp FsCommandPuzzle) run(command string, args ...string) ([]byte, error) {
|
||||||
cmdargs := append([]string{command}, args...)
|
cmdargs := append([]string{command}, args...)
|
||||||
cmd := exec.CommandContext(ctx, "./"+path.Base(fp.command), cmdargs...)
|
cmd := exec.CommandContext(ctx, "./"+path.Base(fp.command), cmdargs...)
|
||||||
cmd.Dir = path.Dir(fp.command)
|
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.
|
// Puzzle returns a Puzzle struct for the current puzzle.
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
______
|
||||||
|
< moo. >
|
||||||
|
------
|
||||||
|
\ ^__^
|
||||||
|
\ (oo)\_______
|
||||||
|
(__)\ )\/\
|
||||||
|
||----w |
|
||||||
|
|| ||
|
|
@ -32,7 +32,7 @@ EOT
|
||||||
echo "Moo."
|
echo "Moo."
|
||||||
;;
|
;;
|
||||||
file:*:*)
|
file:*:*)
|
||||||
fail "No such file: $2"
|
cat "$3" || exit 1
|
||||||
;;
|
;;
|
||||||
answer:1:answer1.0)
|
answer:1:answer1.0)
|
||||||
echo -n '{"Correct":true}'
|
echo -n '{"Correct":true}'
|
||||||
|
|
Loading…
Reference in New Issue