Skip to content

Commit

Permalink
chore: add GH action workflow (#1)
Browse files Browse the repository at this point in the history
* chore: add GH action workflow

* chore: fix tests

* chore: add pnpm in GH actions

* chore: fix workflow

* chore: fix tests on windows

* chore: test npmrc existance
  • Loading branch information
marvinhagemeister authored Feb 22, 2024
1 parent c03a580 commit 8d63641
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Node.js CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
node-version: ["20.x"]

runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- uses: pnpm/action-setup@v3
with:
version: 8

- run: npm i
- run: npm run build --if-present
- run: npm test
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function prettyTime(diff: number) {
}

export async function exec(cmd: string, args: string[], cwd: string) {
const cp = spawn(cmd, args, { stdio: "inherit", cwd });
const cp = spawn(cmd, args, { stdio: "inherit", cwd, shell: true });

return new Promise<void>((resolve) => {
cp.on("exit", (code) => {
Expand Down
20 changes: 14 additions & 6 deletions test/install.test.ts → test/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as path from "path";
import * as fs from "fs";
import { isDirectory, isFile, runJsr, withTempEnv } from "./test_utils";
import * as assert from "node:assert/strict";

describe("install", () => {
it("jsr i @std/encoding - resolve latest version", async () => {
await withTempEnv(["i", "@std/encoding"], async (getPkgJson, dir) => {
const pkgJson = await getPkgJson();
assert(
assert.ok(
pkgJson.dependencies && "@std/encoding" in pkgJson.dependencies,
"Missing dependency entry"
);
Expand All @@ -17,7 +18,14 @@ describe("install", () => {
);

const depPath = path.join(dir, "node_modules", "@std", "encoding");
assert(await isDirectory(depPath), "Not installed in node_modules");
assert.ok(await isDirectory(depPath), "Not installed in node_modules");

const npmrcPath = path.join(dir, ".npmrc");
const npmRc = await fs.promises.readFile(npmrcPath, "utf-8");
assert.ok(
npmRc.includes("@jsr:registry=https://npm.jsr.io"),
"Missing npmrc registry"
);
});
});

Expand Down Expand Up @@ -96,7 +104,7 @@ describe("install", () => {
await withTempEnv(
["i", "--npm", "@std/[email protected]"],
async (_, dir) => {
assert(
assert.ok(
await isFile(path.join(dir, "package-lock.json")),
"npm lockfile not created"
);
Expand All @@ -108,7 +116,7 @@ describe("install", () => {
await withTempEnv(
["i", "--yarn", "@std/[email protected]"],
async (_, dir) => {
assert(
assert.ok(
await isFile(path.join(dir, "yarn.lock")),
"yarn lockfile not created"
);
Expand All @@ -120,7 +128,7 @@ describe("install", () => {
await withTempEnv(
["i", "--pnpm", "@std/[email protected]"],
async (_, dir) => {
assert(
assert.ok(
await isFile(path.join(dir, "pnpm-lock.yaml")),
"pnpm lockfile not created"
);
Expand All @@ -140,7 +148,7 @@ describe("remove", () => {
assert.equal(pkgJson.dependencies, undefined);

const depPath = path.join(dir, "node_modules", "@std", "encoding");
assert(
assert.ok(
!(await isDirectory(depPath)),
"Folder in node_modules not removed"
);
Expand Down
3 changes: 2 additions & 1 deletion test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export interface PkgJson {

export async function runJsr(args: string[], cwd: string) {
const bin = path.join(__dirname, "..", "src", "bin.ts");
return await exec("npx", ["ts-node", bin, ...args], cwd);
const tsNode = path.join(__dirname, "..", "node_modules", ".bin", "ts-node");
return await exec(tsNode, [bin, ...args], cwd);
}

export async function withTempEnv(
Expand Down

0 comments on commit 8d63641

Please sign in to comment.