Skip to content

Commit

Permalink
test: improve reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 3, 2024
1 parent 9b995d2 commit 4557d44
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 63 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
pull_request:
branches: [master, develop]

permissions:
contents: write

jobs:
test:
name: Test
Expand Down Expand Up @@ -34,11 +37,6 @@ jobs:
- name: Type check
run: pnpm type-check

- name: Setup Git # Only used by the last test
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "<>"
- name: Build
run: pnpm build

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"clean-pkg-json": "^1.2.0",
"cleye": "^1.3.2",
"execa": "^8.0.1",
"fs-fixture": "^2.5.0",
"fs-fixture": "^2.6.0",
"lintroll": "^1.10.0",
"manten": "^1.3.0",
"npm-packlist": "^5.1.1",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ const { stringify } = JSON;
const localTemporaryBranch = `git-publish/${publishBranch}-${Date.now()}`;
let success = false;

// Validate remote exists
try {
await execa('git', ['remote', 'get-url', remote]);
} catch {
throw new Error(`Git remote ${stringify(remote)} does not exist`);
}

// In the try-finally block in case it modifies the working tree
// On failure, they will be reverted by the hard reset
try {
Expand Down
94 changes: 42 additions & 52 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
import path from 'path';
import { execa, type Options } from 'execa';
import path from 'node:path';
import { execa } from 'execa';
import { describe, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { createGit } from './utils/create-git.js';

const gitPublish = path.resolve('./dist/index.js');

const createGit = async (cwd: string) => {
const git = (
command: string,
args?: string[],
options?: Options,
) => (
execa(
'git',
[command, ...(args || [])],
{
cwd,
...options,
},
)
);

await git(
'init',
[
// In case of different default branch name
'--initial-branch=master',
],
);

await git('config', ['user.name', 'name']);
await git('config', ['user.email', 'email']);

return git;
};

describe('git-publish', ({ describe, test }) => {
describe('git-publish', ({ describe }) => {
describe('Error cases', ({ test }) => {
test('Fails if not in git repository', async () => {
const fixture = await createFixture();
await using fixture = await createFixture();

const gitPublishProcess = await execa(gitPublish, {
cwd: fixture.path,
Expand All @@ -47,12 +18,10 @@ describe('git-publish', ({ describe, test }) => {

expect(gitPublishProcess.exitCode).toBe(1);
expect(gitPublishProcess.stderr).toBe('Error: Not in a git repository');

await fixture.rm();
});

test('Fails if no package.json found', async () => {
const fixture = await createFixture();
await using fixture = await createFixture();

await createGit(fixture.path);

Expand All @@ -63,12 +32,10 @@ describe('git-publish', ({ describe, test }) => {

expect(gitPublishProcess.exitCode).toBe(1);
expect(gitPublishProcess.stderr).toBe('Error: No package.json found in current working directory');

await fixture.rm();
});

test('Dirty working tree', async () => {
const fixture = await createFixture({
await using fixture = await createFixture({
'package.json': '{}',
});

Expand All @@ -82,24 +49,47 @@ describe('git-publish', ({ describe, test }) => {

expect(gitPublishProcess.exitCode).toBe(1);
expect(gitPublishProcess.stderr).toBe('Error: Working tree is not clean');

await fixture.rm();
});
});

test('Publishes', async ({ onTestFail }) => {
// Requires git config author to be set
// This is set on the GitHub Action because locally, an author already exists

const gitPublishProcess = await execa(gitPublish, {
reject: false,
describe('Current project', async ({ test }) => {
await using fixture = await createFixture(process.cwd(), {
templateFilter: cpPath => !(
cpPath.endsWith(`${path.sep}node_modules`)
|| path.basename(cpPath).startsWith('.')
|| path.basename(cpPath) === 'dist'
),
});

onTestFail(() => {
console.log(gitPublishProcess);
const git = await createGit(fixture.path);
await git('add', ['.']);
await git('commit', ['-am', 'Initial commit']);

await test('Errors on missing remote', async () => {
const gitPublishProcess = await execa(gitPublish, {
cwd: fixture.path,
reject: false,
});

expect(gitPublishProcess.exitCode).toBe(1);
expect(gitPublishProcess.stderr).toBe('Error: Git remote "origin" does not exist');
});

expect(gitPublishProcess.exitCode).toBe(0);
expect(gitPublishProcess.stdout).toMatch('✔');
const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']);
await git('remote', ['add', 'origin', originRemote]);

await test('Publishes', async ({ onTestFail }) => {
const gitPublishProcess = await execa(gitPublish, {
cwd: fixture.path,
reject: false,
});

onTestFail(() => {
console.log(gitPublishProcess);

Check warning on line 88 in tests/index.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
});

expect(gitPublishProcess.exitCode).toBe(0);
expect(gitPublishProcess.stdout).toMatch('✔');
});
});
});
33 changes: 33 additions & 0 deletions tests/utils/create-git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { execa, type Options } from 'execa';

export const createGit = async (
cwd: string,
) => {
const git = (
command: string,
args?: string[],
options?: Options,
) => (
execa(
'git',
[command, ...(args || [])],
{
cwd,
...options,
},
)
);

await git(
'init',
[
// In case of different default branch name
'--initial-branch=master',
],
);

await git('config', ['user.name', 'name']);
await git('config', ['user.email', 'email']);

return git;
};

0 comments on commit 4557d44

Please sign in to comment.