Skip to content

Commit

Permalink
Merge 1b8f0ec into 99c9c22
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Oct 17, 2023
2 parents 99c9c22 + 1b8f0ec commit eb950d0
Show file tree
Hide file tree
Showing 39 changed files with 628 additions and 353 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
- deno.json
- '**/*.ts'
- .github/workflows/ci.yml
push:
branches: main
workflow_dispatch:

concurrency:
Expand All @@ -21,6 +23,7 @@ jobs:
os:
- macos-latest
- ubuntu-latest
- windows-latest

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -58,7 +61,13 @@ jobs:
- run: deno run --no-config --unstable src/mod.ts

dnt:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
Expand Down
8 changes: 5 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
},
"pkgx": "deno^1.33.3 npm",
"tasks": {
"test": "deno test --unstable -A",
"test": "deno test --parallel --unstable -A",
"typecheck": "deno check --unstable ./mod.ts",
"dnt": ".github/deno-to-node.ts"
},
"lint": {
"include": ["src/"]
"include": ["src/"],
"exclude": ["**/*.test.ts"]
},
"test": {
"include": ["src/"],
"exclude": ["dist/"]
},
"imports": {
"is-what": "https://deno.land/x/is_what@v4.1.15/src/index.ts",
"deno/": "https://deno.land/std@0.196.0/"
"deno/": "https://deno.land/std@0.196.0/",
"outdent": "https://deno.land/x/outdent@v0.8.0/mod.ts"
}
}
Empty file modified examples/whisper.ts
100755 → 100644
Empty file.
Binary file added fixtures/foo.com-5.43.0.tgz
Binary file not shown.
1 change: 1 addition & 0 deletions fixtures/projects/bar.com/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
3 changes: 3 additions & 0 deletions fixtures/projects/foo.com/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ runtime:

dependencies:
bar.com: ^1

provides:
- bin/foo
2 changes: 2 additions & 0 deletions fixtures/projects/npmjs.com/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
provides:
- bin/npm
6 changes: 6 additions & 0 deletions fixtures/projects/npmjs.com/provider.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmds:
- truffle
- chalk
args:
chalk: npx --yes --package=chalk-cli --
...: [npx, --yes, --]
9 changes: 9 additions & 0 deletions fixtures/projects/python.org/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provides:
- bin/python

interprets:
extensions: py
args: python

platforms:
- darwin
3 changes: 2 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { moveSync } from "https://deno.land/std@0.196.0/fs/move.ts"
import { readLines } from "https://deno.land/std@0.196.0/io/read_lines.ts"
import { writeAll } from "https://deno.land/std@0.196.0/streams/write_all.ts"
import { parse as parseYaml } from "https://deno.land/std@0.196.0/yaml/parse.ts"
import { SEP } from "https://deno.land/std@0.196.0/path/mod.ts"

const streams = { writeAll }
const io = { readLines }
const fs = { moveSync }
const deno = { readLines, crypto, fs, io, streams, parseYaml }
const deno = { readLines, crypto, fs, io, streams, parseYaml, SEP }

export { deno }
4 changes: 2 additions & 2 deletions src/hooks/useCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Stowage, StowageNativeBottle } from "../types.ts"
import { useTestConfig } from "./useTestConfig.ts"
import { assertEquals } from "deno/assert/mod.ts"
import SemVer from "../utils/semver.ts"
import useConfig from "./useConfig.ts"
import useCache from "./useCache.ts"
import host from "../utils/host.ts"

Deno.test("useCache", () => {
const { cache } = useConfig()
const { cache } = useTestConfig()
const hw = (({ platform, arch }) => `${platform}+${arch}`)(host())

const stowage = StowageNativeBottle({
Expand Down
17 changes: 10 additions & 7 deletions src/hooks/useCellar.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { assertEquals, assertRejects } from "deno/assert/mod.ts"
import SemVer, * as semver from "../utils/semver.ts"
import { useTestConfig } from "./useTestConfig.ts"
import install from "../plumbing/install.ts"
import useCellar from "./useCellar.ts"

Deno.test("useCellar.resolve()", async () => {
useTestConfig()

const pkgrq = { project: "python.org", version: new SemVer("3.11.3")}
const installation = await install(pkgrq)
const pkg = { project: "python.org", version: new SemVer("3.11.3") }
const path = useCellar().shelf(pkg.project).join(`v${pkg.version}`).mkdir('p')
path.join("cant-be-empty").touch()
const installation = { pkg, path }

await useCellar().resolve(installation)
await useCellar().resolve(installation.pkg)
Expand All @@ -21,11 +22,13 @@ Deno.test("useCellar.resolve()", async () => {
Deno.test("useCellar.has()", async () => {
useTestConfig()

const rq = { project: "beyondgrep.com", version: new SemVer("3.6.0") }
const pkg = { project: "beyondgrep.com", version: new SemVer("3.6.0") }

assertEquals(await useCellar().has(rq), undefined)
assertEquals(await useCellar().has(pkg), undefined)

const installation = await install(rq)
const path = useCellar().shelf(pkg.project).join(`v${pkg.version}`).mkdir('p')
path.join("cant-be-empty").touch()
const installation = { pkg, path }

assertEquals(await useCellar().has(rq), installation)
assertEquals(await useCellar().has(pkg), installation)
})
14 changes: 11 additions & 3 deletions src/hooks/useConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ Deno.test("useConfig", () => {
assertEquals(config.UserAgent, "libpkgx")
}

config = ConfigDefault({ PKGX_PANTRY_PATH: "/foo:/bar", CI: "true" })
assertEquals(config.pantries.map(x => x.string), ["/foo", "/bar"])

const PKGX_PANTRY_PATH = Deno.build.os == 'windows' ? "C:\\foo;D:\\bar" : "/foo:/bar"

config = ConfigDefault({ PKGX_PANTRY_PATH, CI: "true" })
if (Deno.build.os == 'windows') {
assertEquals(config.pantries.map(x => x.string), ["C:\\foo", "D:\\bar"])
} else {
assertEquals(config.pantries.map(x => x.string), ["/foo", "/bar"])
}
assertEquals(config.options.compression, "gz")

assertFalse(_internals.boolize("false"))
Expand All @@ -34,6 +41,7 @@ Deno.test("useConfig empty PKGX_DIR is ignored", () => {
})

Deno.test("useConfig empty PKGX_PANTRY_PATH is ignored", () => {
const SEP = Deno.build.os == 'windows' ? ';' : ':'
assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: "" }).pantries, [])
assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: " : :" }).pantries, [])
assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: ` ${SEP} ${SEP}` }).pantries, [])
})
4 changes: 3 additions & 1 deletion src/hooks/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ function platform_cache_default() {
}
}

const SEP = Deno.build.os == 'windows' ? ';' : ':'

export function ConfigDefault(env = Deno.env.toObject()): Config {
const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ?? Path.home().join('.pkgx')
const pantries = env['PKGX_PANTRY_PATH']?.split(":").compact(x => flatmap(x.trim(), x => Path.abs(x) ?? Path.cwd().join(x))) ?? []
const pantries = env['PKGX_PANTRY_PATH']?.split(SEP).compact(x => flatmap(x.trim(), x => Path.abs(x) ?? Path.cwd().join(x))) ?? []
const cache = (flatmap(env["XDG_CACHE_HOME"], x => new Path(x)) ?? platform_cache_default()).join("pkgx")
const isCI = boolize(env['CI']) ?? false
const UserAgent = flatmap(getv(), v => `libpkgx/${v}`) ?? 'libpkgx'
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useDownload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useTestConfig } from "./useTestConfig.ts"
import { assert } from "deno/assert/mod.ts"
import useDownload from "./useDownload.ts"

//TODO don’t actually do http obv.

Deno.test("etag-mtime-check", async runner => {
useTestConfig({ PKGX_DIR: Deno.makeTempDirSync() })

Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ async function the_meat<T>({ src, logger, headers, dst }: DownloadOptions): Prom
}
}

if (logger) logger({ src, dst })

const rsp = await useFetch(src, { headers })

switch (rsp.status) {
Expand Down
44 changes: 22 additions & 22 deletions src/hooks/useFetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { stub, assertSpyCallArgs } from "deno/testing/mock.ts"
import useConfig, { _internals, ConfigDefault } from "./useConfig.ts"
import useFetch from "./useFetch.ts"
import { useTestConfig } from "./useTestConfig.ts";
import useFetch, { _internals } from "./useFetch.ts"


Deno.test("fetch user-agent header check", async () => {
Deno.test({
name: "fetch user-agent header check",
/// doesn't work inside DNT because fetch is shimmed to undici
if (Deno.env.get("NODE")) return
ignore: !!Deno.env.get("NODE"),
async fn() {
const UserAgent = "tests/1.2.3"
useTestConfig({ UserAgent })

const UserAgent = "tests/1.2.3"
const url = "https://example.com";
const fetchStub = stub(
_internals,
"fetch",
() => Promise.resolve(new Response("")),
);

_internals.reset()
useConfig({...ConfigDefault(), UserAgent});
try {
await useFetch(url, {});
} finally {
fetchStub.restore();
}

const url = "https://example.com";
const fetchStub = stub(
globalThis,
"fetch",
() => Promise.resolve(new Response("")),
);

try {
await useFetch(url, {});
} finally {
fetchStub.restore();
assertSpyCallArgs(fetchStub, 0, [url, {
headers: {"User-Agent": UserAgent}
}]);
}

assertSpyCallArgs(fetchStub, 0, [url, {
headers: {"User-Agent": UserAgent}
}]);
});
11 changes: 10 additions & 1 deletion src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ export default function useFetch(input: string | URL | Request, init?: RequestIn
if (UserAgent) {
requestInit.headers = { ...requestInit.headers, "User-Agent": UserAgent }
}
return fetch(input, requestInit)
return _internals.fetch(input, requestInit)
}

// wrapped or DNT chokes on typechecking
function chew(input: string | URL | Request, init?: RequestInit | undefined): Promise<Response> {
return fetch(input, init)
}

export const _internals = {
fetch: chew
}
6 changes: 1 addition & 5 deletions src/hooks/useInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DownloadError } from "./useDownload.ts"
import SemVer from "../utils/semver.ts"
import useFetch from "./useFetch.ts"
import host from "../utils/host.ts"
import Path from "../utils/Path.ts"
import "../utils/misc.ts"

export interface Inventory {
Expand All @@ -26,10 +25,7 @@ const select = async (rq: PackageRequirement | Package) => {

const get = async (rq: PackageRequirement | Package) => {
const { platform, arch } = host()

const url = new URL('https://dist.pkgx.dev')
url.pathname = Path.root.join(rq.project, platform, arch, 'versions.txt').string

const url = new URL(`https://dist.pkgx.dev/${rq.project}/${platform}/${arch}/versions.txt`)
const rsp = await useFetch(url)

if (!rsp.ok) {
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useOffLicense.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Stowage } from "../types.ts"
import host from "../utils/host.ts"
import Path from "../utils/Path.ts"

type Type = 's3'

Expand All @@ -9,18 +8,19 @@ export default function useOffLicense(_type: Type) {
}

function key(stowage: Stowage) {
let rv = Path.root.join(stowage.pkg.project)
const rv = [stowage.pkg.project]
if (stowage.type == 'bottle') {
const { platform, arch } = stowage.host ?? host()
rv = rv.join(`${platform}/${arch}`)
rv.push(`${platform}/${arch}`)
}
let fn = `v${stowage.pkg.version}`
if (stowage.type == 'bottle') {
fn += `.tar.${stowage.compression}`
} else {
fn += stowage.extname
}
return rv.join(fn).string.slice(1)
rv.push(fn)
return rv.join("/")
}

function url(stowage: Stowage) {
Expand Down
24 changes: 4 additions & 20 deletions src/hooks/usePantry.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
import { assert, assertEquals, assertThrows } from "deno/assert/mod.ts"
import usePantry, { validatePackageRequirement } from "./usePantry.ts"
import { _internals as _config_internals } from "./useConfig.ts"
import { useTestConfig } from "./useTestConfig.ts"
import { useTestConfig, srcroot } from "./useTestConfig.ts"
import { _internals } from "../utils/host.ts"
import { stub } from "deno/testing/mock.ts"
import SemVer from "../utils/semver.ts"
import Path from "../utils/Path.ts"
import useSync from "./useSync.ts"

Deno.test("provides()", async () => {
useTestConfig()
await useSync()
const exenames = await usePantry().project("python.org").provides()
assert(exenames.includes("python"))
})

Deno.test("which()", async () => {
useTestConfig()
await useSync()
const pkg = await usePantry().which({ interprets: ".py" })
assertEquals(pkg?.project, "python.org")
})

Deno.test("provider()", async () => {
useTestConfig()
await useSync()
const provides = await usePantry().project("npmjs.com").provider()
const foo = provides!('truffle')
assertEquals(foo![0], 'npx')
})

Deno.test("available()", async () => {
useTestConfig()
await useSync()
const stubber = stub(_internals, 'platform', () => "darwin" as "darwin" | "linux")
assert(await usePantry().project("agpt.co").available())
assert(await usePantry().project("python.org").available())
stubber.restore()
})

Deno.test("runtime.env", async () => {
const srcroot = (() => {
// because when running via dnt the path of this file is different
if (Path.cwd().parent().parent().join("fixtures").isDirectory()) {
return Path.cwd().parent().parent()
} else {
return new Path(new URL(import.meta.url).pathname).parent().parent().parent()
}
})()
const PKGX_PANTRY_PATH = srcroot.join("fixtures").string
const { prefix } = useTestConfig({ PKGX_PANTRY_PATH })

Expand All @@ -61,12 +46,11 @@ Deno.test("runtime.env", async () => {
const env = await usePantry().project("foo.com").runtime.env(new SemVer("2.3.4"), deps)

assertEquals(env.BAZ, prefix.join("bar.com/v1.2.3/baz").string)

_config_internals.reset()
})

Deno.test("missing()", () => {
useTestConfig({PKGX_PANTRY_PATH: "/a"})
useTestConfig()
usePantry().prefix.rm({ recursive: true })
assert(usePantry().missing())
})

Expand Down
Loading

0 comments on commit eb950d0

Please sign in to comment.