<commit_before>package chroot

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"polydawn.net/repeatr/def"
	"polydawn.net/repeatr/input"
	"polydawn.net/repeatr/testutil"
)

func Test(t *testing.T) {
	Convey("Given a rootfs input that errors", t,
		testutil.WithTmpdir(func() {
			formula := def.Formula{
				Inputs: []def.Input{
					{
						Type:     "tar",
						Location: "/",
						URI:      "/nonexistance/in/its/most/essential/unform.tar.gz",
					},
				},
			}
			executor := &Executor{
				workspacePath: "chroot_workspace",
			}
			So(os.Mkdir(executor.workspacePath, 0755), ShouldBeNil)

			Convey("We should get an InputError", func() {
				So(func() { executor.Run(formula) }, testutil.ShouldPanicWith, input.InputError)
			})
		}),
	)

	projPath, _ := os.Getwd()
	projPath = filepath.Dir(filepath.Dir(projPath))

	testutil.Convey_IfHaveRoot("Given a rootfs", t,
		testutil.WithTmpdir(func() {
			formula := def.Formula{
				Inputs: []def.Input{
					{
						Type:     "tar",
						Location: "/",
						URI:      filepath.Join(projPath, "assets/ubuntu.tar.gz"),
					},
				},
			}
			executor := &Executor{
				workspacePath: "chroot_workspace",
			}
			So(os.Mkdir(executor.workspacePath, 0755), ShouldBeNil)

			Convey("The executor should be able to invoke echo", func() {
				formula.Accents = def.Accents{
					Entrypoint: []string{"echo", "echococo"},
				}

				job, _ := executor.Run(formula)
				So(job, ShouldNotBeNil)
				So(job.ExitCode(), ShouldEqual, 0) // TODO: this waits... the test should still pass if the reader happens first
				msg, err := ioutil.ReadAll(job.OutputReader())
				So(err, ShouldBeNil)
				So(string(msg), ShouldEqual, "echococo\n")
			})

			Convey("The executor should be able to check exit codes", func() {
				formula.Accents = def.Accents{
					Entrypoint: []string{"sh", "-c", "exit 14"},
				}

				job, _ := executor.Run(formula)
				So(job, ShouldNotBeNil)
				So(job.ExitCode(), ShouldEqual, 14)
			})

			Convey("The executor should report command not found clearly", func() {
				formula.Accents = def.Accents{
					Entrypoint: []string{"not a command"},
				}

				So(func() { executor.Run(formula) }, testutil.ShouldPanicWith, NoSuchCommandError)
			})

			Convey("Given another input", func() {
				formula.Inputs = append(formula.Inputs, def.Input{
					Type:     "dir",
					Location: "/data/test",
					URI:      "testdata",
				})
				So(os.Mkdir("testdata", 0755), ShouldBeNil)
				So(ioutil.WriteFile("testdata/1", []byte{}, 0644), ShouldBeNil)
				So(ioutil.WriteFile("testdata/2", []byte{}, 0644), ShouldBeNil)
				So(ioutil.WriteFile("testdata/3", []byte{}, 0644), ShouldBeNil)
				// FIXME: ugh, you either need some fixtures up in here, or we need to implement a "scan" feature asap

				//	Convey("The executor should be able to see the mounted files", func() {
				//		formula.Accents = def.Accents{
				//			Entrypoint: []string{"ls", "/data/test"},
				//		}
				//
				//		job, _ := executor.Run(formula)
				//		So(job, ShouldNotBeNil)
				//		So(job.ExitCode(), ShouldEqual, 0)
				//		msg, err := ioutil.ReadAll(job.OutputReader())
				//		So(err, ShouldBeNil)
				//		So(string(msg), ShouldEqual, "1 2 3\n")
				//	})
			})
		}),
	)
}<commit_msg>Use input fixtures to get a chroot executor test that demos inputs appearing in the rightful place.<commit_after>package chroot

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"polydawn.net/repeatr/def"
	"polydawn.net/repeatr/input"
	"polydawn.net/repeatr/input/fixtures"
	"polydawn.net/repeatr/testutil"
)

func TestMain(m *testing.M) {
	code := m.Run()
	inputfixtures.Cleanup()
	os.Exit(code)
}

func Test(t *testing.T) {
	Convey("Given a rootfs input that errors", t,
		testutil.WithTmpdir(func() {
			formula := def.Formula{
				Inputs: []def.Input{
					{
						Type:     "tar",
						Location: "/",
						URI:      "/nonexistance/in/its/most/essential/unform.tar.gz",
					},
				},
			}
			executor := &Executor{
				workspacePath: "chroot_workspace",
			}
			So(os.Mkdir(executor.workspacePath, 0755), ShouldBeNil)

			Convey("We should get an InputError", func() {
				So(func() { executor.Run(formula) }, testutil.ShouldPanicWith, input.InputError)
			})
		}),
	)

	projPath, _ := os.Getwd()
	projPath = filepath.Dir(filepath.Dir(projPath))

	testutil.Convey_IfHaveRoot("Given a rootfs", t,
		testutil.WithTmpdir(func() {
			formula := def.Formula{
				Inputs: []def.Input{
					{
						Type:     "tar",
						Location: "/",
						URI:      filepath.Join(projPath, "assets/ubuntu.tar.gz"),
					},
				},
			}
			executor := &Executor{
				workspacePath: "chroot_workspace",
			}
			So(os.Mkdir(executor.workspacePath, 0755), ShouldBeNil)

			Convey("The executor should be able to invoke echo", func() {
				formula.Accents = def.Accents{
					Entrypoint: []string{"echo", "echococo"},
				}

				job, _ := executor.Run(formula)
				So(job, ShouldNotBeNil)
				So(job.ExitCode(), ShouldEqual, 0) // TODO: this waits... the test should still pass if the reader happens first
				msg, err := ioutil.ReadAll(job.OutputReader())
				So(err, ShouldBeNil)
				So(string(msg), ShouldEqual, "echococo\n")
			})

			Convey("The executor should be able to check exit codes", func() {
				formula.Accents = def.Accents{
					Entrypoint: []string{"sh", "-c", "exit 14"},
				}

				job, _ := executor.Run(formula)
				So(job, ShouldNotBeNil)
				So(job.ExitCode(), ShouldEqual, 14)
			})

			Convey("The executor should report command not found clearly", func() {
				formula.Accents = def.Accents{
					Entrypoint: []string{"not a command"},
				}

				So(func() { executor.Run(formula) }, testutil.ShouldPanicWith, NoSuchCommandError)
			})

			Convey("Given another input", func() {
				inputfixtures.DirInput2.Location = "/data/test"
				formula.Inputs = append(formula.Inputs, inputfixtures.DirInput2)

				Convey("The executor should be able to see the mounted files", func() {
					formula.Accents = def.Accents{
						Entrypoint: []string{"ls", "/data/test"},
					}

					job, _ := executor.Run(formula)
					So(job, ShouldNotBeNil)
					So(job.ExitCode(), ShouldEqual, 0)
					msg, err := ioutil.ReadAll(job.OutputReader())
					So(err, ShouldBeNil)
					So(string(msg), ShouldEqual, "1\n2\n3\n")
				})
			})
		}),
	)
}
{'fraction_non_alphanumeric': 0.15881983678593847, 'fraction_numerical': 0.008631512868801004, 'mean_word_length': 3.10896196002579, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5436481', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2274, 'n_words': 462}