<commit_before>package elicit_test

import (
	"fmt"
	"io/ioutil"
	"mmatt/elicit"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strings"
	"testing"
)

var tempdir string

func Test(t *testing.T) {
	elicit.New().
		WithSpecsFolder("./specs").
		WithTransforms(transforms).
		WithSteps(steps).
		BeforeScenarios(createTempDir).
		AfterScenarios(removeTempDir).
		RunTests(t)
}

var steps = elicit.Steps{}
var transforms = elicit.Transforms{}

func init() {
	steps["Create a temporary environment"] =
		func(t *testing.T) {
			createFile(t, "specs_test.go", testfile)
		}

	steps["Create an? `(.*)` file:"] =
		func(t *testing.T, filename string, text elicit.TextBlock) {
			createFile(t, filename, text.Content)
		}

	steps["Create (?:a step definition|step definitions|transform definitions):"] =
		func(t *testing.T, text elicit.TextBlock) {
			createFile(t, "steps_test.go", fmt.Sprintf(stepFileFmt, "", text.Content))
		}

	steps["Create (?:a step definition|step definitions) using (.+):"] =
		func(t *testing.T, imports []string, text elicit.TextBlock) {
			createFile(t, "steps_test.go", fmt.Sprintf(stepFileFmt, strings.Join(imports, "\n"), text.Content))
		}

	steps["Running `(go test.*)` will output:"] =
		func(t *testing.T, command string, text elicit.TextBlock) {
			output := runGoTest(t, command)

			expected, actual := quoteOutput(text.Content), quoteOutput(output)
			if !strings.Contains(actual, expected) {
				t.Errorf("\n\nExpected:\n\n%s\n\nto contain:\n\n%s\n", actual, expected)
			}
		}

	steps["Running `(go test.*)` will output the following lines:"] =
		func(t *testing.T, command string, text elicit.TextBlock) {
			output := runGoTest(t, command)

			missingLines := []string{}
			for _, line := range strings.Split(text.Content, "\n") {
				if !strings.Contains(output, line) {
					missingLines = append(missingLines, line)
				}
			}

			if len(missingLines) > 0 {
				t.Errorf("\n\nExpected:\n\n%s\n\nto contain the lines:\n\n%s\n",
					quoteOutput(output),
					quoteOutput(strings.Join(missingLines, "\n")))
			}
		}

	steps["`(.+)` will contain:"] =
		func(t *testing.T, filename string, text elicit.TextBlock) {
			path := filepath.Join(tempdir, filename)

			if _, err := os.Stat(path); os.IsNotExist(err) {
				t.Error(filename, err)
			}

			if contents, err := ioutil.ReadFile(path); err != nil {
				t.Error("reading", filename, err)
			} else {
				actual := string(contents)
				expected := strings.TrimSpace(text.Content)
				if actual != expected {
					t.Errorf("\n\nExpected:\n\n%s\n\nto equal:\n\n%s\n", quoteOutput(actual), quoteOutput(expected))
				}
			}
		}
}

func createTempDir() {
	var err error
	tempdir, err = ioutil.TempDir("", "elicit_test")

	if err != nil {
		panic(fmt.Errorf("creating tempdir: %s", err))
	}
}

func removeTempDir() {
	if err := os.RemoveAll(tempdir); err != nil {
		panic(fmt.Errorf("removing tempdir %q: %s", tempdir, err))
	}
}

func createFile(t *testing.T, filename, contents string) {
	if tempdir == "" {
		t.Fatal("creating file: tempdir not set")
	}

	outpath := filepath.Join(tempdir, filename)

	if _, err := os.Stat(outpath); os.IsNotExist(err) {
		ioutil.WriteFile(outpath, []byte(contents), 0777)
	} else {
		t.Fatal("creating file:", outpath, "already exists")
	}
}<commit_msg>Restore Working Directory After Each Run<commit_after>package elicit_test

import (
	"fmt"
	"io/ioutil"
	"mmatt/elicit"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strings"
	"testing"
)

var startdir, tempdir string

func init() {
	wd, err := os.Getwd()
	if err != nil {
		panic(fmt.Errorf("os.Getwd(): %s", err))
	}
	startdir = wd
}

func Test(t *testing.T) {
	elicit.New().
		WithSpecsFolder("./specs").
		WithTransforms(transforms).
		WithSteps(steps).
		BeforeScenarios(createTempDir).
		AfterScenarios(removeTempDir).
		RunTests(t)
}

var steps = elicit.Steps{}
var transforms = elicit.Transforms{}

func init() {
	steps["Create a temporary environment"] =
		func(t *testing.T) {
			createFile(t, "specs_test.go", testfile)
		}

	steps["Create an? `(.*)` file:"] =
		func(t *testing.T, filename string, text elicit.TextBlock) {
			createFile(t, filename, text.Content)
		}

	steps["Create (?:a step definition|step definitions|transform definitions):"] =
		func(t *testing.T, text elicit.TextBlock) {
			createFile(t, "steps_test.go", fmt.Sprintf(stepFileFmt, "", text.Content))
		}

	steps["Create (?:a step definition|step definitions) using (.+):"] =
		func(t *testing.T, imports []string, text elicit.TextBlock) {
			createFile(t, "steps_test.go", fmt.Sprintf(stepFileFmt, strings.Join(imports, "\n"), text.Content))
		}

	steps["Running `(go test.*)` will output:"] =
		func(t *testing.T, command string, text elicit.TextBlock) {
			output := runGoTest(t, command)

			expected, actual := quoteOutput(text.Content), quoteOutput(output)
			if !strings.Contains(actual, expected) {
				t.Errorf("\n\nExpected:\n\n%s\n\nto contain:\n\n%s\n", actual, expected)
			}
		}

	steps["Running `(go test.*)` will output the following lines:"] =
		func(t *testing.T, command string, text elicit.TextBlock) {
			output := runGoTest(t, command)

			missingLines := []string{}
			for _, line := range strings.Split(text.Content, "\n") {
				if !strings.Contains(output, line) {
					missingLines = append(missingLines, line)
				}
			}

			if len(missingLines) > 0 {
				t.Errorf("\n\nExpected:\n\n%s\n\nto contain the lines:\n\n%s\n",
					quoteOutput(output),
					quoteOutput(strings.Join(missingLines, "\n")))
			}
		}

	steps["`(.+)` will contain:"] =
		func(t *testing.T, filename string, text elicit.TextBlock) {
			path := filepath.Join(tempdir, filename)

			if _, err := os.Stat(path); os.IsNotExist(err) {
				t.Error(filename, err)
			}

			if contents, err := ioutil.ReadFile(path); err != nil {
				t.Error("reading", filename, err)
			} else {
				actual := string(contents)
				expected := strings.TrimSpace(text.Content)
				if actual != expected {
					t.Errorf("\n\nExpected:\n\n%s\n\nto equal:\n\n%s\n", quoteOutput(actual), quoteOutput(expected))
				}
			}
		}
}

func createTempDir() {
	var err error
	tempdir, err = ioutil.TempDir("", "elicit_test")

	if err != nil {
		panic(fmt.Errorf("creating tempdir: %s", err))
	}
}

func removeTempDir() {
	if err := os.RemoveAll(tempdir); err != nil {
		panic(fmt.Errorf("removing tempdir %q: %s", tempdir, err))
	}
	if err := os.Chdir(startdir); err != nil {
		panic(fmt.Errorf("reverting wd to %q: %s", startdir, err))
	}
}

func createFile(t *testing.T, filename, contents string) {
	if tempdir == "" {
		t.Fatal("creating file: tempdir not set")
	}

	outpath := filepath.Join(tempdir, filename)

	if _, err := os.Stat(outpath); os.IsNotExist(err) {
		ioutil.WriteFile(outpath, []byte(contents), 0777)
	} else {
		t.Fatal("creating file:", outpath, "already exists")
	}
}
{'fraction_non_alphanumeric': 0.17315317961521515, 'fraction_numerical': 0.00146864444118079, 'mean_word_length': 3.9527272727272726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1623370', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2458, 'n_words': 527}