moth/cmd/transpile/main_test.go

99 lines
2.3 KiB
Go
Raw Normal View History

2020-08-28 17:41:17 -06:00
package main
import (
"bytes"
"encoding/json"
"testing"
2020-09-08 17:49:02 -06:00
"github.com/dirtbags/moth/pkg/transpile"
2020-08-28 17:41:17 -06:00
"github.com/spf13/afero"
)
var testMothYaml = []byte(`---
answers:
- YAML answer
pre:
authors:
- Arthur
- Buster
- DW
2020-09-04 18:28:23 -06:00
attachments:
- filename: moo.txt
2020-08-28 17:41:17 -06:00
---
YAML body
`)
func newTestFs() afero.Fs {
fs := afero.NewMemMapFs()
2020-08-31 16:37:51 -06:00
afero.WriteFile(fs, "cat0/1/puzzle.md", testMothYaml, 0644)
2020-09-14 13:40:55 -06:00
afero.WriteFile(fs, "cat0/1/moo.txt", []byte("Moo."), 0644)
2020-09-11 13:03:19 -06:00
afero.WriteFile(fs, "cat0/2/puzzle.moth", testMothYaml, 0644)
2020-08-28 17:41:17 -06:00
afero.WriteFile(fs, "cat0/3/puzzle.moth", testMothYaml, 0644)
2020-08-31 16:37:51 -06:00
afero.WriteFile(fs, "cat0/4/puzzle.md", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/5/puzzle.md", testMothYaml, 0644)
2020-09-11 13:03:19 -06:00
afero.WriteFile(fs, "cat0/10/puzzle.md", testMothYaml, 0644)
2020-09-04 18:28:23 -06:00
afero.WriteFile(fs, "unbroken/1/puzzle.md", testMothYaml, 0644)
2020-09-14 13:40:55 -06:00
afero.WriteFile(fs, "unbroken/1/moo.txt", []byte("Moo."), 0644)
2020-09-11 13:03:19 -06:00
afero.WriteFile(fs, "unbroken/2/puzzle.md", testMothYaml, 0644)
2020-09-14 13:40:55 -06:00
afero.WriteFile(fs, "unbroken/2/moo.txt", []byte("Moo."), 0644)
2020-08-28 17:41:17 -06:00
return fs
}
2020-09-11 13:03:19 -06:00
func (tp T) Run(args ...string) error {
tp.Args = append([]string{"transpile"}, args...)
command, err := tp.ParseArgs()
if err != nil {
return err
}
return command()
}
2020-09-01 20:12:57 -06:00
func TestEverything(t *testing.T) {
2020-08-28 17:41:17 -06:00
stdout := new(bytes.Buffer)
2020-09-11 13:03:19 -06:00
stderr := new(bytes.Buffer)
2020-08-28 17:41:17 -06:00
tp := T{
2020-09-11 13:03:19 -06:00
Stdout: stdout,
Stderr: stderr,
BaseFs: newTestFs(),
2020-08-28 17:41:17 -06:00
}
2020-09-11 13:03:19 -06:00
if err := tp.Run("inventory"); err != nil {
2020-08-28 17:41:17 -06:00
t.Error(err)
}
2020-09-11 13:03:19 -06:00
if stdout.String() != "cat0 1 2 3 4 5 10\nunbroken 1 2\n" {
2020-08-28 17:41:17 -06:00
t.Errorf("Bad inventory: %#v", stdout.String())
}
stdout.Reset()
2020-09-11 13:03:19 -06:00
if err := tp.Run("open", "-dir=cat0/1"); err != nil {
2020-08-28 17:41:17 -06:00
t.Error(err)
}
2020-09-08 17:49:02 -06:00
p := transpile.Puzzle{}
2020-08-28 17:41:17 -06:00
if err := json.Unmarshal(stdout.Bytes(), &p); err != nil {
t.Error(err)
}
2020-09-01 20:12:57 -06:00
if (len(p.Answers) != 1) || (p.Answers[0] != "YAML answer") {
t.Error("Didn't return the right object", p)
2020-08-28 17:41:17 -06:00
}
stdout.Reset()
2020-09-11 13:03:19 -06:00
if err := tp.Run("open", "-dir=cat0/1", "-file=moo.txt"); err != nil {
2020-08-28 17:41:17 -06:00
t.Error(err)
}
if stdout.String() != "Moo." {
2020-09-14 13:40:55 -06:00
t.Error("Wrong file pulled", stdout.String())
2020-08-28 17:41:17 -06:00
}
2020-09-04 18:28:23 -06:00
stdout.Reset()
2020-09-14 13:40:55 -06:00
if err := tp.Run("mothball", "-dir=unbroken"); err != nil {
2020-09-11 13:03:19 -06:00
t.Log(tp.fs)
2020-09-04 18:28:23 -06:00
t.Error(err)
}
if stdout.Len() < 200 {
t.Error("That's way too short to be a mothball")
}
if stdout.String()[:2] != "PK" {
t.Error("This mothball isn't a zip file!")
}
2020-08-28 17:41:17 -06:00
}