Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tevanoff committed May 13, 2024
1 parent be49a77 commit e368c0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
38 changes: 36 additions & 2 deletions node-src/git/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { execaCommand } from 'execa';
import { describe, expect, it, vi } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { getCommit, getSlug, hasPreviousCommit, mergeQueueBranchMatch } from './git';
import { NULL_BYTE, findFilesFromRepositoryRoot, getCommit, getSlug, hasPreviousCommit, mergeQueueBranchMatch } from './git';

vi.mock('execa');

const command = vi.mocked(execaCommand);

afterEach(() => {
vi.clearAllMocks();
})

describe('getCommit', () => {
it('parses log output', async () => {
command.mockImplementation(
Expand Down Expand Up @@ -106,3 +110,33 @@ describe('mergeQueueBranchMatch', () => {
);
});
});

describe('findFilesFromRepositoryRoot', () => {
it('finds files relative to the repository root', async () => {
const filesFound = [
'package.json',
'another/package/package.json',
];

// first call from getRepositoryRoot()
command.mockImplementationOnce(
() =>
Promise.resolve({
all: '/root',
}) as any
);

command.mockImplementationOnce(
() =>
Promise.resolve({
all: filesFound.join(NULL_BYTE),
}) as any
);

const results = await findFilesFromRepositoryRoot('package.json', '**/package.json');

expect(command).toBeCalledTimes(2);
expect(command).toHaveBeenNthCalledWith(2, "git ls-files --full-name -z '/root/package.json' '/root/**/package.json'", expect.any(Object));
expect(results).toEqual(filesFound);
});
});
3 changes: 2 additions & 1 deletion node-src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import gitNotInstalled from '../ui/messages/errors/gitNotInstalled';
import path from 'node:path';

const newline = /\r\n|\r|\n/; // Git may return \n even on Windows, so we can't use EOL
export const NULL_BYTE = '\0'; // Separator used when running `git ls-files` with `-z`

export async function execGitCommand(command: string) {
try {
Expand Down Expand Up @@ -295,7 +296,7 @@ export async function findFilesFromRepositoryRoot(...patterns: string[]) {
// not the directory in which this is executed from
const gitCommand = `git ls-files --full-name -z ${patternsFromRoot.map((p) => `'${p}'`).join(' ')}`;
const files = await execGitCommand(gitCommand);
return files.split('\0').filter(Boolean);
return files.split(NULL_BYTE).filter(Boolean);
}

export async function mergeQueueBranchMatch(branch) {
Expand Down

0 comments on commit e368c0f

Please sign in to comment.