Skip to content

Commit 9037e1d

Browse files
committed
Add test to verify we fixed libtea useConfig use
Wild bug where separate instances of libtea were loaded that thus did not share config. Fixed in libtea 0.1.5, but theses tests fail before 0.1.5 and now pass.
1 parent 70b58f2 commit 9037e1d

4 files changed

Lines changed: 53 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</p>
1717

1818

19-
# tea/cli 0.33.1
19+
# tea/cli 0.33.2
2020

2121
`tea` puts the whole open source ecosystem at your fingertips:
2222

tests/integration.suite.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type RunOptions = ({
1616
} | {
1717
cmd: string[]
1818
}) & {
19-
env?: Record<string, string>
19+
env?: Record<string, string | null>
2020
throws?: boolean
2121
}
2222

@@ -35,6 +35,8 @@ const suite = describe({
3535
const tmp = new Path(await Deno.makeTempDir({ prefix: "tea-" }))
3636
const cwd = new Path(new URL(import.meta.url).pathname).parent().parent()
3737

38+
assert(cwd.join("deno.jsonc").isFile())
39+
3840
//TODO use deno task compile, however seems to be a bug where we cannot control the output location
3941
const proc = Deno.run({
4042
cmd: [
@@ -71,7 +73,7 @@ const suite = describe({
7173

7274
async beforeEach(this: This) {
7375
const tmp = new Path(await Deno.makeTempDir({ prefix: "tea-" }))
74-
const TEA_PREFIX = existing_tea_prefix ?? tmp.join('opt').mkdir()
76+
const TEA_PREFIX = tmp.join('opt').mkdir()
7577

7678
this.TEA_PREFIX = TEA_PREFIX
7779
assert(this.TEA_PREFIX.isDirectory())
@@ -87,10 +89,15 @@ const suite = describe({
8789
if (value) env[key] = value
8890
}
8991
env['PATH'] = `${this.tea.parent()}:/usr/bin:/bin` // these systems are full of junk so we prune PATH
90-
env['TEA_PREFIX'] ??= TEA_PREFIX.string
92+
if (env['TEA_PREFIX'] === undefined) env['TEA_PREFIX'] = TEA_PREFIX.string
93+
if (env['TEA_PANTRY_PATH'] === undefined) env['TEA_PANTRY_PATH'] = this.TEA_PANTRY_PATH.string
94+
if (env['TEA_CACHE_DIR'] === undefined) env['TEA_CACHE_DIR'] = this.TEA_CACHE_DIR.string
95+
9196
env['CLICOLOR_FORCE'] = '0'
92-
env['TEA_PANTRY_PATH'] ??= this.TEA_PANTRY_PATH.string
93-
env['TEA_CACHE_DIR'] ??= this.TEA_CACHE_DIR.string
97+
98+
for (const [key, value] of Object.entries(env)) {
99+
if (value === null) delete env[key]
100+
}
94101

95102
let stdout: "piped" | undefined
96103
let stderr: "piped" | undefined
@@ -106,7 +113,7 @@ const suite = describe({
106113
cmd.unshift(this.tea.string)
107114
}
108115

109-
const proc = Deno.run({ cmd, cwd: sandbox.string, stdout, stderr, env, clearEnv: true})
116+
const proc = Deno.run({ cmd, cwd: sandbox.string, stdout, stderr, env: env as any, clearEnv: true})
110117
try {
111118
const status = await proc.status()
112119
if ((throws === undefined || throws) && !status.success) {

tests/integration/cli.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { assert, assertEquals, assertMatch } from "deno/testing/asserts.ts"
1+
import { assert, assertEquals, assertMatch, assertRejects } from "deno/testing/asserts.ts"
22
import suite from "../integration.suite.ts"
33
import { it } from "deno/testing/bdd.ts"
4+
import undent from "outdent"
45

56
it(suite, "tea --help", async function() {
67
const out = await this.run({args: ["--help"]}).stdout()
@@ -44,3 +45,23 @@ it(suite, "tea http-server --help (via npx provider)", async function() {
4445
it(suite, "tea pkg --version", async function() {
4546
await this.run({ args: ["pkg", "--version"] })
4647
})
48+
49+
it(suite, "tea determines its own prefix and pantry works", async function() {
50+
// this verifies that useConfig() works as expected even though seemingly
51+
// deno loads separate copies of libtea for each of the import map methods we use
52+
// history: there was a bug where libtea internally had a different config object than tea/cli
53+
// testing the pantry works properly verifies that this is no longer the case
54+
55+
this.tea = this.tea.mv({ into: this.TEA_PREFIX.join("tea.xyz/v0.33.2/bin").mkdir('p') })
56+
57+
this.TEA_PREFIX
58+
.join("tea.xyz/var/pantry/projects/foo.com").mkdir('p')
59+
.join("package.yml").write({ text: undent`
60+
provides: [bin/foo]
61+
`})
62+
63+
const stdout = await this.run({ args: ["--prefix"], env: { TEA_PREFIX: null, TEA_PANTRY_PATH: null } }).stdout()
64+
assertEquals(stdout.trim(), this.TEA_PREFIX.string)
65+
66+
await this.run({ args: ["--provides", "foo"], env: { TEA_PREFIX: null, TEA_PANTRY_PATH: null } })
67+
})

tests/unit/useConfig.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import useConfig, { ConfigDefault } from "../../src/hooks/useConfig.ts"
22
import { assertEquals } from "deno/testing/asserts.ts"
33
import { _internals } from "tea/hooks/useConfig.ts"
4-
import { Path } from "tea"
4+
import { Path, hooks } from "tea"
5+
const { usePantry } = hooks
56

67
Deno.test("prefix discovery", async () => {
78
const tmp = new Path(await Deno.makeTempDir())
@@ -37,3 +38,18 @@ Deno.test("prefix discovery defaults to ~/.tea", async () => {
3738
const config = ConfigDefault(undefined, tea.string, {})
3839
assertEquals(config.prefix, Path.home().join(".tea"))
3940
})
41+
42+
Deno.test("useConfig global state is shared across import styles", async () => {
43+
// testing for a bug where imports of `"tea"` and `"tea/hooks/useConfig.ts"` would
44+
// load their own copies of the global config object
45+
46+
const tmp = new Path(await Deno.makeTempDir())
47+
const tea = tmp.join('tea.xyz/v0.33.2/bin').mkdir('p').join('tea').touch()
48+
49+
_internals.reset()
50+
const defaults = ConfigDefault(undefined, tea.string, {})
51+
const config = useConfig(defaults)
52+
53+
assertEquals(config.prefix, tmp)
54+
assertEquals(usePantry().prefix, tmp.join("tea.xyz/var/pantry/projects"))
55+
})

0 commit comments

Comments
 (0)