mirror of https://github.com/dirtbags/moth.git
Get all tests passing
This commit is contained in:
parent
484b4e3f5c
commit
2391ebe27b
|
@ -73,7 +73,6 @@ func (t *T) ParseArgs() (Command, error) {
|
||||||
} else {
|
} else {
|
||||||
t.fs = t.BaseFs
|
t.fs = t.BaseFs
|
||||||
}
|
}
|
||||||
log.Println(t.Args, t.fs)
|
|
||||||
|
|
||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
@ -107,7 +106,6 @@ func (t *T) DumpFile() error {
|
||||||
|
|
||||||
switch t.filename {
|
switch t.filename {
|
||||||
case "puzzle.json", "":
|
case "puzzle.json", "":
|
||||||
// BUG(neale): we need a way to tell the transpiler to strip answers
|
|
||||||
p, err := puzzle.Puzzle()
|
p, err := puzzle.Puzzle()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/dirtbags/moth/pkg/transpile"
|
"github.com/dirtbags/moth/pkg/transpile"
|
||||||
|
@ -27,21 +26,22 @@ YAML body
|
||||||
func newTestFs() afero.Fs {
|
func newTestFs() afero.Fs {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
afero.WriteFile(fs, "cat0/1/puzzle.md", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/1/puzzle.md", testMothYaml, 0644)
|
||||||
afero.WriteFile(fs, "cat0/1/moo.txt", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/1/moo.txt", []byte("Moo."), 0644)
|
||||||
afero.WriteFile(fs, "cat0/2/puzzle.moth", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/2/puzzle.moth", testMothYaml, 0644)
|
||||||
afero.WriteFile(fs, "cat0/3/puzzle.moth", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/3/puzzle.moth", testMothYaml, 0644)
|
||||||
afero.WriteFile(fs, "cat0/4/puzzle.md", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/4/puzzle.md", testMothYaml, 0644)
|
||||||
afero.WriteFile(fs, "cat0/5/puzzle.md", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/5/puzzle.md", testMothYaml, 0644)
|
||||||
afero.WriteFile(fs, "cat0/10/puzzle.md", testMothYaml, 0644)
|
afero.WriteFile(fs, "cat0/10/puzzle.md", testMothYaml, 0644)
|
||||||
afero.WriteFile(fs, "unbroken/1/puzzle.md", testMothYaml, 0644)
|
afero.WriteFile(fs, "unbroken/1/puzzle.md", testMothYaml, 0644)
|
||||||
|
afero.WriteFile(fs, "unbroken/1/moo.txt", []byte("Moo."), 0644)
|
||||||
afero.WriteFile(fs, "unbroken/2/puzzle.md", testMothYaml, 0644)
|
afero.WriteFile(fs, "unbroken/2/puzzle.md", testMothYaml, 0644)
|
||||||
|
afero.WriteFile(fs, "unbroken/2/moo.txt", []byte("Moo."), 0644)
|
||||||
return fs
|
return fs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tp T) Run(args ...string) error {
|
func (tp T) Run(args ...string) error {
|
||||||
tp.Args = append([]string{"transpile"}, args...)
|
tp.Args = append([]string{"transpile"}, args...)
|
||||||
command, err := tp.ParseArgs()
|
command, err := tp.ParseArgs()
|
||||||
log.Println(tp.fs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -81,12 +81,11 @@ func TestEverything(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if stdout.String() != "Moo." {
|
if stdout.String() != "Moo." {
|
||||||
t.Error("Wrong file pulled")
|
t.Error("Wrong file pulled", stdout.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.Reset()
|
stdout.Reset()
|
||||||
if err := tp.Run("mothball", "-dir=cat0"); err != nil {
|
if err := tp.Run("mothball", "-dir=unbroken"); err != nil {
|
||||||
t.Log(tp.BaseFs)
|
|
||||||
t.Log(tp.fs)
|
t.Log(tp.fs)
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,16 @@ type RecursiveBasePathFs struct {
|
||||||
|
|
||||||
// NewRecursiveBasePathFs returns a new RecursiveBasePathFs.
|
// NewRecursiveBasePathFs returns a new RecursiveBasePathFs.
|
||||||
func NewRecursiveBasePathFs(source afero.Fs, path string) *RecursiveBasePathFs {
|
func NewRecursiveBasePathFs(source afero.Fs, path string) *RecursiveBasePathFs {
|
||||||
return &RecursiveBasePathFs{
|
ret := &RecursiveBasePathFs{
|
||||||
Fs: afero.NewBasePathFs(source, path),
|
|
||||||
source: source,
|
source: source,
|
||||||
path: path,
|
path: path,
|
||||||
}
|
}
|
||||||
|
if path == "" {
|
||||||
|
ret.Fs = source
|
||||||
|
} else {
|
||||||
|
ret.Fs = afero.NewBasePathFs(source, path)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// RealPath returns the real path to a file, "breaking out" of the RecursiveBasePathFs.
|
// RealPath returns the real path to a file, "breaking out" of the RecursiveBasePathFs.
|
||||||
|
|
|
@ -63,6 +63,12 @@ func TestOsFsCategory(t *testing.T) {
|
||||||
t.Error("Wrong authors", p.Pre.Authors)
|
t.Error("Wrong authors", p.Pre.Authors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p, err := static.Puzzle(3); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if len(p.Pre.Authors) != 1 {
|
||||||
|
t.Error("Wrong authors", p.Pre.Authors)
|
||||||
|
}
|
||||||
|
|
||||||
generated := NewFsCategory(fs, "generated")
|
generated := NewFsCategory(fs, "generated")
|
||||||
|
|
||||||
if inv, err := generated.Inventory(); err != nil {
|
if inv, err := generated.Inventory(); err != nil {
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Mothball packages a Category up for a production server run.
|
// Mothball packages a Category up for a production server run.
|
||||||
|
@ -36,7 +35,6 @@ func Mothball(c Category) (*bytes.Reader, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Println(puzzlePath)
|
|
||||||
puzzle, err := c.Puzzle(points)
|
puzzle, err := c.Puzzle(points)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -3,18 +3,32 @@ package transpile
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/afero/zipfs"
|
"github.com/spf13/afero/zipfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMothballs(t *testing.T) {
|
func TestMothballsMemFs(t *testing.T) {
|
||||||
|
static := NewFsCategory(newTestFs(), "cat1")
|
||||||
|
if _, err := Mothball(static); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMothballsOsFs(t *testing.T) {
|
||||||
|
_, testfn, _, _ := runtime.Caller(0)
|
||||||
|
os.Chdir(path.Dir(testfn))
|
||||||
|
|
||||||
fs := NewRecursiveBasePathFs(afero.NewOsFs(), "testdata")
|
fs := NewRecursiveBasePathFs(afero.NewOsFs(), "testdata")
|
||||||
static := NewFsCategory(fs, "static")
|
static := NewFsCategory(fs, "static")
|
||||||
mb, err := Mothball(static)
|
mb, err := Mothball(static)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mbr, err := zip.NewReader(mb, int64(mb.Len()))
|
mbr, err := zip.NewReader(mb, int64(mb.Len()))
|
||||||
|
|
|
@ -338,14 +338,18 @@ type FsCommandPuzzle struct {
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Puzzle returns a Puzzle struct for the current puzzle.
|
func (fp FsCommandPuzzle) run(args ...string) ([]byte, error) {
|
||||||
func (fp FsCommandPuzzle) Puzzle() (Puzzle, error) {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, fp.command)
|
cmd := exec.CommandContext(ctx, "./"+path.Base(fp.command), args...)
|
||||||
cmd.Dir = path.Dir(fp.command)
|
cmd.Dir = path.Dir(fp.command)
|
||||||
stdout, err := cmd.Output()
|
return cmd.Output()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Puzzle returns a Puzzle struct for the current puzzle.
|
||||||
|
func (fp FsCommandPuzzle) Puzzle() (Puzzle, error) {
|
||||||
|
stdout, err := fp.run()
|
||||||
if exiterr, ok := err.(*exec.ExitError); ok {
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
||||||
return Puzzle{}, errors.New(string(exiterr.Stderr))
|
return Puzzle{}, errors.New(string(exiterr.Stderr))
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -375,13 +379,8 @@ func (c nopCloser) Close() error {
|
||||||
// Open returns a newly-opened file.
|
// Open returns a newly-opened file.
|
||||||
// BUG(neale): FsCommandPuzzle.Open() reads everything into memory, and will suck for large files.
|
// BUG(neale): FsCommandPuzzle.Open() reads everything into memory, and will suck for large files.
|
||||||
func (fp FsCommandPuzzle) Open(filename string) (ReadSeekCloser, error) {
|
func (fp FsCommandPuzzle) Open(filename string) (ReadSeekCloser, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
|
stdout, err := fp.run("--file", filename)
|
||||||
defer cancel()
|
buf := nopCloser{bytes.NewReader(stdout)}
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, fp.command, "--file", filename)
|
|
||||||
cmd.Dir = path.Dir(fp.command)
|
|
||||||
out, err := cmd.Output()
|
|
||||||
buf := nopCloser{bytes.NewReader(out)}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return buf, err
|
return buf, err
|
||||||
}
|
}
|
||||||
|
@ -391,18 +390,13 @@ func (fp FsCommandPuzzle) Open(filename string) (ReadSeekCloser, error) {
|
||||||
|
|
||||||
// Answer checks whether the given answer is correct.
|
// Answer checks whether the given answer is correct.
|
||||||
func (fp FsCommandPuzzle) Answer(answer string) bool {
|
func (fp FsCommandPuzzle) Answer(answer string) bool {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
|
stdout, err := fp.run("--answer", answer)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, fp.command, "--answer", answer)
|
|
||||||
cmd.Dir = path.Dir(fp.command)
|
|
||||||
out, err := cmd.Output()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERROR: checking answer: %s", err)
|
log.Printf("ERROR: checking answer: %s", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
switch strings.TrimSpace(string(out)) {
|
switch strings.TrimSpace(string(stdout)) {
|
||||||
case "correct":
|
case "correct":
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,19 @@ func TestPuzzle(t *testing.T) {
|
||||||
if p.Pre.Body != "<p>YAML body</p>\n" {
|
if p.Pre.Body != "<p>YAML body</p>\n" {
|
||||||
t.Errorf("Body parsed wrong: %#v", p.Pre.Body)
|
t.Errorf("Body parsed wrong: %#v", p.Pre.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f, err := pd.Open("moo.txt")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if _, err := io.Copy(buf, f); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if buf.String() != "Moo." {
|
||||||
|
t.Error("Attachment wrong: ", buf.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue