Skip to content

Commit

Permalink
Interpret .node-version contents as @ prefixed (pkgxdev#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Apr 19, 2023
1 parent 3cabcf3 commit e55578c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</p>


# tea/cli 0.28.2
# tea/cli 0.28.3

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

Expand Down
14 changes: 9 additions & 5 deletions src/hooks/useVirtualEnv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { usePackageYAMLFrontMatter, refineFrontMatter, FrontMatter } from "./usePackageYAML.ts"
import { flatmap, TeaError, validate_plain_obj } from "utils"
import { flatmap, pkg, TeaError, validate_plain_obj } from "utils"
import { useEnv, useMoustaches, usePrefix } from "hooks"
import { PackageRequirement } from "types"
import SemVer, * as semver from "semver"
Expand Down Expand Up @@ -132,11 +132,15 @@ export default async function(cwd: Path = Path.cwd()): Promise<VirtualEnv> {
}
}
if (_if(".node-version")) {
// https://github.com/shadowspawn/node-version-usage
let s = (await f!.read()).trim()
if (s.startsWith('v')) s = s.slice(1) // is allowed
const constraint = semver.Range.parse(s)
if (!constraint) throw new Error('couldn’t parse: .node-version')
pkgs.push({ project: "nodejs.org", constraint })
if (s.startsWith('v')) s = s.slice(1) // v prefix has no effect but is allowed
s = `nodejs.org@${s}`
try {
pkgs.push(pkg.parse(s))
} catch {
throw new Error('couldn’t parse: .node-version')
}
}
if (_if("package.json")) {
const json = JSON.parse(await f!.read())
Expand Down
18 changes: 11 additions & 7 deletions tests/functional/devenv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Deno.test("should provide packages in dev env", { sanitizeResources: false, sani
const SHELL = "/bin/zsh"

const tests = [
{ file: ".node-version", pkg: "nodejs.org~16.16" },
{ file: ".node-version", pkg: "nodejs.org>=16.16<16.16.1" },
{ file: "action.yml", pkg: "nodejs.org^16" },
{ file: "README.md", pkg: "nodejs.org=16.16.0" }
]
Expand All @@ -124,16 +124,20 @@ Deno.test("should provide packages in dev env", { sanitizeResources: false, sani
}
})

Deno.test("tolerant .node-version parsing", { sanitizeResources: false, sanitizeOps: false }, async () => {
Deno.test("tolerant .node-version parsing", { sanitizeResources: false, sanitizeOps: false }, async test => {
const SHELL = "/bin/zsh"

const {run, teaDir } = await createTestHarness()
teaDir.join(".node-version").write({ text: "\n\n\nv16\n" })
for (const [spec, interpretation] of [["v16", "^16"], ["v16.16", "~16.16"], ["v16.16.0", ">=16.16<16.16.1"]]) {
await test.step(spec, async () => {
const {run, teaDir } = await createTestHarness()
teaDir.join(".node-version").write({ text: `\n\n\n${spec}\n` })

const { stdout } = await run(["+tea.xyz/magic", "-Esk", "--chaste", "env"], { env: { SHELL } })
const { stdout } = await run(["+tea.xyz/magic", "-Esk", "--chaste", "env"], { env: { SHELL } })

const pkg = "nodejs.org^16"
assert(getTeaPackages(SHELL, stdout).includes(pkg), "should include nodejs dep")
const pkg = `nodejs.org${interpretation}`
assert(getTeaPackages(SHELL, stdout).includes(pkg), "should include nodejs dep")
})
}
})

function getEnvVar(shell: string, lines: string[], key: string): string | null {
Expand Down

0 comments on commit e55578c

Please sign in to comment.