Skip to content

Commit bd92c50

Browse files
committed
test(docs): isolate environment examples
1 parent 0688731 commit bd92c50

4 files changed

Lines changed: 33 additions & 18 deletions

File tree

‎docs/README.md‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ cases or a program.
181181
### Cases
182182

183183
An expression followed by a `//` comment is a case: the comment is the value the expression renders
184-
as inside a container, so a string is quoted: `"ant".toUpper() // "ANT"`. A block with a case is a block of cases, like a notebook of inputs and
185-
outputs. The block is read as the REPL reads input, so a case may span lines; its comment goes at
186-
the end of its last line.
184+
as inside a container, so a string is quoted: `"ant".toUpper() // "ANT"`. A block with a case is a
185+
block of cases, like a notebook of inputs and outputs. The block is read as the REPL reads input, so
186+
a case may span lines; its comment goes at the end of its last line.
187187

188188
```ant
189189
Array.map([1, 2, 3], { x * 2 }) // [2, 4, 6]
@@ -269,17 +269,17 @@ the next ` ```ant ` block. A misspelled prefix (`tests:`, `Test:`), an unknown d
269269
directive of the same name on one block (`file` and `env` aside), and a directive that does not
270270
apply to its block all fail the test.
271271

272-
| Directive | Meaning | Applies to |
273-
| --------------------------- | --------------------------------------------------------------------- | -------------------------------- |
274-
| `test: file <path>` + body | Write the body to `<path>` in the page's directory. | any block |
275-
| `test: before` + body | Ant code put in front of the block, hidden from the reader. | cases, an unnamed program |
276-
| `test: after` + body | Ant code put after the block, hidden from the reader. | an unnamed program |
277-
| `test: stdin` + body | Feed the body to standard input. | a block that runs |
278-
| `test: args <a> <b>` | Pass arguments, which `Os.args()` returns. | an unnamed program without `cmd` |
279-
| `test: env <NAME>=<value>` | Set an environment variable for the `ant` process; may repeat. | a block that runs |
280-
| `test: cmd ant <args>` | Run this `ant` command in the page's directory, such as `ant test .`. | a program |
281-
| `test: exit <n>` | Expect exit status `<n>` instead of 0. | a block that runs |
282-
| `test: check-only <reason>` | Check and format-check only. For output that changes between runs. | an ant program, not with `cmd` |
272+
| Directive | Meaning | Applies to |
273+
| ---------------------------- | --------------------------------------------------------------------- | -------------------------------- |
274+
| `test: file <path>` + body | Write the body to `<path>` in the page's directory. | any block |
275+
| `test: before` + body | Ant code put in front of the block, hidden from the reader. | cases, an unnamed program |
276+
| `test: after` + body | Ant code put after the block, hidden from the reader. | an unnamed program |
277+
| `test: stdin` + body | Feed the body to standard input. | a block that runs |
278+
| `test: args <a> <b>` | Pass arguments, which `Os.args()` returns. | an unnamed program without `cmd` |
279+
| `test: env <NAME>[=<value>]` | Set an environment variable, or unset it without `=`; may repeat. | a block that runs |
280+
| `test: cmd ant <args>` | Run this `ant` command in the page's directory, such as `ant test .`. | a program |
281+
| `test: exit <n>` | Expect exit status `<n>` instead of 0. | a block that runs |
282+
| `test: check-only <reason>` | Check and format-check only. For output that changes between runs. | an ant program, not with `cmd` |
283283

284284
A block runs when it is an unnamed program, or a named one with `cmd`, has no `check-only`, and
285285
marks no line with `// error:`. `exit` and `// panic:` exclude each other.

‎docs/howto/cli/env.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Call [Os.env](../../lib/Os/env.md). It returns `null` when the variable is not s
1111
default with `??`:
1212

1313
<!-- test: env EDITOR=nano -->
14+
<!-- test: env PAGER -->
1415

1516
```ant
1617
let editor = Os.env("EDITOR") ?? "vi"
@@ -77,6 +78,7 @@ the `Err` that [Result.fail](../../lib/Result/fail.md) makes from the script, wh
7778
[String.length](../../lib/String/length.md) counts what it got:
7879

7980
<!-- test: exit 1 -->
81+
<!-- test: env API_TOKEN -->
8082

8183
```ant
8284
let token = Os.env("API_TOKEN") ?? return Result.fail("API_TOKEN is not set")

‎tests/docs_harness_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// contract is itself tested: every page that should fail names the problem, and every page that
1010
// should pass does.
1111
func TestDocsHarness(t *testing.T) {
12+
t.Setenv("ANT_DOC_PRESENT", "from parent")
1213
const f = "```"
1314
draft := "<!-- status: draft -->\n\n"
1415
cases := []struct {
@@ -35,6 +36,7 @@ func TestDocsHarness(t *testing.T) {
3536
{name: "a function in cases sees a let", page: f + "ant\nlet magic = 1\nfunc f(): int {\n magic\n}\nf() // 1\n" + f + "\n"},
3637
{name: "modules and tests", page: f + "ant [lib/geo.ant]\nexport func double(n: int): int { n * 2 }\n" + f + "\n\n" + f + "ant\nimport \"./lib/geo.ant\" { double }\n\nprintln(double(21))\n" + f + "\n\n" + f + "text\n42\n" + f + "\n\n<!-- test: cmd ant test . -->\n\n" + f + "ant [geo_test.ant]\nimport \"./lib/geo.ant\" { double }\n\ntest \"double\" {\n assert(double(2) == 4)\n}\n" + f + "\n"},
3738
{name: "env", page: "<!-- test: env ANT_DOC_A=one two -->\n<!-- test: env ANT_DOC_B= -->\n\n" + f + "ant\nprintln(Os.env(\"ANT_DOC_A\"))\nprintln(Os.env(\"ANT_DOC_B\") == \"\")\n" + f + "\n\n" + f + "text\none two\ntrue\n" + f + "\n"},
39+
{name: "unset env", page: "<!-- test: env ANT_DOC_PRESENT -->\n<!-- test: env ANT_DOC_A=temporary -->\n<!-- test: env ANT_DOC_A -->\n\n" + f + "ant\nprintln(Os.env(\"ANT_DOC_PRESENT\") ?? \"absent\")\nprintln(Os.env(\"ANT_DOC_A\") == null)\n" + f + "\n\n" + f + "text\nabsent\ntrue\n" + f + "\n"},
3840
{name: "check-only", page: "<!-- test: check-only prints the time -->\n\n" + f + "ant\nprintln(Time.now())\n" + f + "\n"},
3941
{name: "indented fence", page: "1. Run it:\n\n " + f + "ant\n println(1)\n " + f + "\n\n " + f + "text\n 1\n " + f + "\n"},
4042
{name: "odd but local file name", page: "<!-- test: file a..b.txt\nx\n-->\n\n" + f + "ant\nprintln(Result.unwrap(File.read(\"a..b.txt\")))\n" + f + "\n"},
@@ -72,7 +74,7 @@ func TestDocsHarness(t *testing.T) {
7274
{name: "a link to the spec", page: "[narrowing](https://github.com/ant-src/spec/blob/master/ant-lang.md#narrowing)\n", want: []string{"names the spec"}},
7375
{name: "mistyped directives", page: "<!-- tests: exit 3 -->\n<!-- Test: exit 3 -->\n\n" + f + "ant\nprintln(1)\n" + f + "\n", want: []string{"p.md:3: a directive starts with test: exactly", "p.md:4: a directive starts"}},
7476
{name: "unknown directive", page: "<!-- test: frobnicate -->\n\n" + f + "ant\nprintln(1)\n" + f + "\n", want: []string{"unknown directive"}},
75-
{name: "env needs a name", page: "<!-- test: env NOVALUE -->\n<!-- test: env =x -->\n\n" + f + "ant\nprintln(1)\n" + f + "\n", want: []string{"test: env needs NAME=value, not \"NOVALUE\"", "not \"=x\""}},
77+
{name: "env needs a name", page: "<!-- test: env BAD NAME -->\n<!-- test: env =x -->\n\n" + f + "ant\nprintln(1)\n" + f + "\n", want: []string{"test: env needs NAME or NAME=value, not \"BAD NAME\"", "not \"=x\""}},
7678
{name: "env on a module", page: "<!-- test: env A=1 -->\n\n" + f + "ant [lib.ant]\nexport func f(): int { 1 }\n" + f + "\n", want: []string{"test: env does not apply"}},
7779
{name: "body on the head line", page: "<!-- test: stdin hello -->\n\n" + f + "ant\nprintln(1)\n" + f + "\n", want: []string{"takes no argument"}},
7880
{name: "duplicate directive", page: "<!-- test: before\nvar a = 1\n-->\n<!-- test: before\nvar b = 2\n-->\n\n" + f + "ant\nprintln(1)\n" + f + "\n", want: []string{"a second test: before"}},

‎tests/docs_test.go‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"regexp"
1212
"runtime"
13+
"slices"
1314
"strconv"
1415
"strings"
1516
"testing"
@@ -671,8 +672,8 @@ func blockProblems(t *testing.T, bin, rel, dir string, b *docBlock) []string {
671672
}
672673
var env []string
673674
for _, d := range b.directives {
674-
if name, _, ok := strings.Cut(d.arg, "="); d.name == "env" && (!ok || name == "") {
675-
problems = append(problems, fmt.Sprintf("%s:%d: test: env needs NAME=value, not %q", rel, d.line, d.arg))
675+
if name, _, _ := strings.Cut(d.arg, "="); d.name == "env" && (name == "" || strings.ContainsAny(name, " \t")) {
676+
problems = append(problems, fmt.Sprintf("%s:%d: test: env needs NAME or NAME=value, not %q", rel, d.line, d.arg))
676677
} else if d.name == "env" {
677678
env = append(env, d.arg)
678679
}
@@ -721,7 +722,17 @@ func blockProblems(t *testing.T, bin, rel, dir string, b *docBlock) []string {
721722
// The built ant comes first on PATH, so a page that starts ant itself gets this one and not
722723
// whatever the host has under the name, such as Apache Ant.
723724
path := filepath.Dir(bin) + string(os.PathListSeparator) + os.Getenv("PATH")
724-
cmd.Env = append(append(os.Environ(), "PATH="+path), env...)
725+
cmd.Env = append(os.Environ(), "PATH="+path)
726+
for _, e := range env {
727+
name, _, set := strings.Cut(e, "=")
728+
cmd.Env = slices.DeleteFunc(cmd.Env, func(v string) bool {
729+
key, _, _ := strings.Cut(v, "=")
730+
return key == name || runtime.GOOS == "windows" && strings.EqualFold(key, name)
731+
})
732+
if set {
733+
cmd.Env = append(cmd.Env, e)
734+
}
735+
}
725736
cmd.Stdout, cmd.Stderr = &stdout, &stderr
726737
status := 0
727738
if err := cmd.Run(); err != nil {

0 commit comments

Comments
 (0)