testmark-go

testexec: selftests (!) for both the filesystem provisioning and the then-subtest feature.

commit 23ec2b65f42ee40f9d3c7e51061c4ba2e59f9d6c · parent aa3d1eab87 · browse this tree
Eric Myhre, 2021-10-21 19:46:04 UTC

One small fix, which makes sure it's fine for subtests to not
continue to add files using the file fixture mechanism,
which previously threw an NPE (whoopsie).
diff --git a/testexec/selfexercise.md b/testexec/selfexercise.md
new file mode 100644
index 0000000..99fa451
--- /dev/null
+++ b/testexec/selfexercise.md
@@ -0,0 +1,81 @@
+testexec selfexercise file
+==========================
+
+We will run a script with some files provisioned:
+
+[testmark]:# (whee/fs/a)
+```
+body-a
+```
+
+The script is simple:
+
+[testmark]:# (whee/script)
+```
+echo hello
+ls
+```
+
+The output is unsurprising:
+
+[testmark]:# (whee/output)
+```
+hello
+a
+```
+
+---
+
+We can make subtests which add more files:
+
+[testmark]:# (whee/then-more-files/fs/b)
+```
+body-b
+```
+
+[testmark]:# (whee/then-more-files/script)
+```
+ls
+```
+
+[testmark]:# (whee/then-more-files/output)
+```
+a
+b
+```
+
+---
+
+We can also affect the filesystem in subtests:
+
+[testmark]:# (whee/then-touching-files/script)
+```
+echo "body-c" > ./c
+ls
+```
+
+Notice that this doesn't inherit the file "b" from the last test --
+this is because they're siblings, so they each got a copy of the *parent* filesystem state.
+
+[testmark]:# (whee/then-touching-files/output)
+```
+a
+c
+```
+
+---
+
+If we make a subtest of *that* subtest, it also inherits a copy of those effects:
+
+[testmark]:# (whee/then-touching-files/then-subtesting-again/script)
+```
+ls
+cat ./c
+```
+
+[testmark]:# (whee/then-touching-files/then-subtesting-again/output)
+```
+a
+c
+body-c
+```
diff --git a/testexec/testexec.go b/testexec/testexec.go
index 4e13da0..d7214bc 100644
--- a/testexec/testexec.go
+++ b/testexec/testexec.go
@@ -205,8 +205,10 @@ func (tcfg Tester) test(t *testing.T, data testmark.DirEnt, allowExec, allowScri
 		}
 
 		// Create any new files.
-		if err := createFiles(fsEnt, "."); err != nil {
-			t.Errorf("test aborted: could not populate files to tempdir: %s", err)
+		if fsEnt != nil {
+			if err := createFiles(fsEnt, "."); err != nil {
+				t.Errorf("test aborted: could not populate files to tempdir: %s", err)
+			}
 		}
 	}
 
diff --git a/testexec/testexec_test.go b/testexec/testexec_test.go
new file mode 100644
index 0000000..0701f4a
--- /dev/null
+++ b/testexec/testexec_test.go
@@ -0,0 +1,28 @@
+package testexec_test
+
+import (
+	"testing"
+
+	"github.com/warpfork/go-testmark"
+	"github.com/warpfork/go-testmark/testexec"
+)
+
+func Test(t *testing.T) {
+	filename := "selfexercise.md"
+	doc, err := testmark.ReadFile(filename)
+	if err != nil {
+		t.Fatalf("spec file parse failed?!: %s", err)
+	}
+
+	doc.BuildDirIndex()
+	patches := testmark.PatchAccumulator{}
+	for _, dir := range doc.DirEnt.ChildrenList {
+		t.Run(dir.Name, func(t *testing.T) {
+			test := testexec.Tester{
+				Patches: &patches,
+			}
+			test.TestScript(t, dir)
+		})
+	}
+	patches.WriteFileWithPatches(doc, filename)
+}