Skip to content

Commit

Permalink
move .git exists check up so that we can avoid mocking it out
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jun 24, 2021
1 parent dc87559 commit 2c69643
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
15 changes: 10 additions & 5 deletions packages/kbn-optimizer/src/optimizer/cache_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ export interface OptimizerCacheKey {
}

async function getLastCommit() {
if (!Fs.existsSync(Path.join(REPO_ROOT, '.git'))) {
return undefined;
}

const { stdout } = await execa(
'git',
['log', '-n', '1', '--pretty=format:%H', '--', RELATIVE_DIR],
Expand All @@ -133,7 +129,16 @@ async function getLastCommit() {
return stdout.trim() || undefined;
}

export async function getOptimizerCacheKey(config: OptimizerConfig) {
export async function getOptimizerCacheKey(config: OptimizerConfig): Promise<OptimizerCacheKey> {
if (!Fs.existsSync(Path.resolve(REPO_ROOT, '.git'))) {
return {
lastCommit: undefined,
modifiedTimes: {},
workerConfig: config.getCacheableWorkerConfig(),
deletedPaths: [],
};
}

const [changes, lastCommit] = await Promise.all([
getChanges(RELATIVE_DIR),
getLastCommit(),
Expand Down
22 changes: 12 additions & 10 deletions packages/kbn-optimizer/src/optimizer/get_changes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
*/

jest.mock('execa');
jest.mock('fs');

import { getChanges } from './get_changes';
import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils';

const execa: jest.Mock = jest.requireMock('execa');

expect.addSnapshotSerializer(createAbsolutePathSerializer());

it('parses git ls-files output', async () => {
expect.assertions(4);

jest.requireMock('fs').existsSync.mockImplementation(() => true);

execa.mockImplementation((cmd, args, options) => {
expect(cmd).toBe('git');
expect(args).toEqual(['ls-files', '-dmt', '--', '/foo/bar/x']);
expect(args).toEqual(['ls-files', '-dmt', '--', 'foo/bar/x']);
expect(options).toEqual({
cwd: '/foo/bar/x',
cwd: REPO_ROOT,
});

return {
Expand All @@ -37,12 +37,14 @@ it('parses git ls-files output', async () => {
};
});

await expect(getChanges('/foo/bar/x')).resolves.toMatchInlineSnapshot(`
const changes = await getChanges('foo/bar/x');

expect(changes).toMatchInlineSnapshot(`
Map {
"/foo/bar/x/kbn-optimizer/package.json" => "modified",
"/foo/bar/x/kbn-optimizer/src/common/bundle.ts" => "modified",
"/foo/bar/x/kbn-optimizer/src/common/bundles.ts" => "deleted",
"/foo/bar/x/kbn-optimizer/src/get_bundle_definitions.test.ts" => "deleted",
<absolute path>/kbn-optimizer/package.json => "modified",
<absolute path>/kbn-optimizer/src/common/bundle.ts => "modified",
<absolute path>/kbn-optimizer/src/common/bundles.ts => "deleted",
<absolute path>/kbn-optimizer/src/get_bundle_definitions.test.ts => "deleted",
}
`);
});
5 changes: 0 additions & 5 deletions packages/kbn-optimizer/src/optimizer/get_changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Path from 'path';

import execa from 'execa';
import fs from 'fs';

import { REPO_ROOT } from '@kbn/dev-utils';

Expand All @@ -21,10 +20,6 @@ export type Changes = Map<string, 'modified' | 'deleted'>;
export async function getChanges(relativeDir: string) {
const changes: Changes = new Map();

if (!fs.existsSync(Path.join(REPO_ROOT, '.git'))) {
return changes;
}

const { stdout } = await execa('git', ['ls-files', '-dmt', '--', relativeDir], {
cwd: REPO_ROOT,
});
Expand Down

0 comments on commit 2c69643

Please sign in to comment.