2020-08-28 17:41:17 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-12 17:44:44 -06:00
|
|
|
"archive/zip"
|
2020-08-28 17:41:17 -06:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-10-20 13:10:24 -06:00
|
|
|
"fmt"
|
2020-10-12 17:44:44 -06:00
|
|
|
"io/ioutil"
|
2021-10-20 13:10:24 -06:00
|
|
|
"os"
|
2020-11-02 09:21:19 -07:00
|
|
|
"strings"
|
2020-08-28 17:41:17 -06:00
|
|
|
"testing"
|
|
|
|
|
2023-04-11 17:56:59 -06:00
|
|
|
"github.com/dirtbags/moth/v4/pkg/transpile"
|
2020-08-28 17:41:17 -06:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testMothYaml = []byte(`---
|
|
|
|
answers:
|
|
|
|
- YAML answer
|
2021-02-24 16:34:35 -07:00
|
|
|
authors:
|
|
|
|
- Arthur
|
|
|
|
- Buster
|
|
|
|
- DW
|
|
|
|
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-10-27 13:19:38 -06:00
|
|
|
func TestTranspilerEverything(t *testing.T) {
|
2021-02-25 15:56:23 -07:00
|
|
|
stdin := new(bytes.Buffer)
|
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{
|
2021-02-25 15:56:23 -07:00
|
|
|
Stdin: stdin,
|
2020-09-11 13:03:19 -06:00
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
BaseFs: newTestFs(),
|
2020-08-28 17:41:17 -06:00
|
|
|
}
|
|
|
|
|
2021-03-26 18:20:49 -06:00
|
|
|
if err := tp.Run("inventory", "-dir=cat0"); err != nil {
|
2020-08-28 17:41:17 -06:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-03-26 18:20:49 -06:00
|
|
|
if stdout.String() != "{\"Puzzles\":[1,2,3,4,5,10]}" {
|
2020-08-28 17:41:17 -06:00
|
|
|
t.Errorf("Bad inventory: %#v", stdout.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout.Reset()
|
2020-10-27 13:19:38 -06:00
|
|
|
if err := tp.Run("puzzle", "-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()
|
2021-03-25 20:40:07 -06:00
|
|
|
if err := tp.Run("file", "-dir=cat0/1", "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-10-27 13:19:38 -06:00
|
|
|
|
|
|
|
stdout.Reset()
|
2021-03-25 20:40:07 -06:00
|
|
|
if err := tp.Run("answer", "-dir=cat0/1", "YAML answer"); err != nil {
|
2020-10-27 13:19:38 -06:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if stdout.String() != `{"Correct":true}` {
|
|
|
|
t.Error("Answer validation failed", stdout.String())
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:56:23 -07:00
|
|
|
stdout.Reset()
|
|
|
|
stdin.Reset()
|
|
|
|
stdin.WriteString("text *emphasized* text")
|
|
|
|
if err := tp.Run("markdown"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if stdout.String() != "<p>text <em>emphasized</em> text</p>\n" {
|
|
|
|
t.Error("Markdown conversion failed", stdout.String())
|
|
|
|
}
|
2020-10-12 17:44:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMothballs(t *testing.T) {
|
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
tp := T{
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
BaseFs: newTestFs(),
|
|
|
|
}
|
2020-09-04 18:28:23 -06:00
|
|
|
|
|
|
|
stdout.Reset()
|
2021-03-25 20:40:07 -06:00
|
|
|
if err := tp.Run("mothball", "-dir=unbroken", "unbroken.mb"); err != nil {
|
2020-10-12 17:44:44 -06:00
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// afero.WriteFile(tp.BaseFs, "unbroken.mb", []byte("moo"), 0644)
|
|
|
|
fis, err := afero.ReadDir(tp.BaseFs, "/")
|
|
|
|
if err != nil {
|
2020-09-04 18:28:23 -06:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-10-12 17:44:44 -06:00
|
|
|
for _, fi := range fis {
|
|
|
|
t.Log(fi.Name())
|
2020-09-04 18:28:23 -06:00
|
|
|
}
|
2020-10-12 17:44:44 -06:00
|
|
|
|
|
|
|
mb, err := tp.BaseFs.Open("unbroken.mb")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer mb.Close()
|
|
|
|
|
|
|
|
info, err := mb.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
zmb, err := zip.NewReader(mb, info.Size())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, zf := range zmb.File {
|
|
|
|
f, err := zf.Open()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
buf, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch zf.Name {
|
|
|
|
case "answers.txt":
|
|
|
|
if len(buf) == 0 {
|
|
|
|
t.Error("answers.txt empty")
|
|
|
|
}
|
|
|
|
case "puzzles.txt":
|
|
|
|
if len(buf) == 0 {
|
|
|
|
t.Error("puzzles.txt empty")
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 18:28:23 -06:00
|
|
|
}
|
2020-08-28 17:41:17 -06:00
|
|
|
}
|
2020-11-02 09:21:19 -07:00
|
|
|
|
|
|
|
func TestFilesystem(t *testing.T) {
|
2021-02-25 15:56:23 -07:00
|
|
|
stdin := new(bytes.Buffer)
|
2020-11-02 09:21:19 -07:00
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
tp := T{
|
2021-02-25 15:56:23 -07:00
|
|
|
Stdin: stdin,
|
2020-11-02 09:21:19 -07:00
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
BaseFs: afero.NewOsFs(),
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:27:19 -07:00
|
|
|
stdout.Reset()
|
|
|
|
if err := tp.Run("puzzle", "-dir=testdata/cat1/1"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(stdout.String(), "moo") {
|
|
|
|
t.Error("File not pulled from cwd", stdout.String())
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:21:19 -07:00
|
|
|
stdout.Reset()
|
2021-03-25 20:40:07 -06:00
|
|
|
if err := tp.Run("file", "-dir=testdata/cat1/1", "moo.txt"); err != nil {
|
2020-11-02 09:21:19 -07:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(stdout.String(), "Moo.") {
|
|
|
|
t.Error("Wrong file pulled", stdout.String())
|
|
|
|
}
|
2021-02-24 16:34:35 -07:00
|
|
|
}
|
2021-10-20 13:10:24 -06:00
|
|
|
|
|
|
|
func TestCwd(t *testing.T) {
|
|
|
|
testwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Can't get current working directory!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Chdir(testwd)
|
|
|
|
|
|
|
|
stdin := new(bytes.Buffer)
|
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
tp := T{
|
|
|
|
Stdin: stdin,
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
BaseFs: afero.NewOsFs(),
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout.Reset()
|
|
|
|
os.Chdir("/")
|
|
|
|
if err := tp.Run(
|
|
|
|
"file",
|
|
|
|
fmt.Sprintf("-dir=%s/testdata/cat1/1", testwd),
|
|
|
|
"moo.txt",
|
|
|
|
); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|