From 2c6964300ec97bce6b52587d8d92faa1ce4f968b Mon Sep 17 00:00:00 2001 From: spalger Date: Thu, 24 Jun 2021 15:00:41 -0700 Subject: [PATCH] move .git exists check up so that we can avoid mocking it out --- .../kbn-optimizer/src/optimizer/cache_keys.ts | 15 ++++++++----- .../src/optimizer/get_changes.test.ts | 22 ++++++++++--------- .../src/optimizer/get_changes.ts | 5 ----- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/kbn-optimizer/src/optimizer/cache_keys.ts b/packages/kbn-optimizer/src/optimizer/cache_keys.ts index d89cb04e9dd12..da06b76327a8b 100644 --- a/packages/kbn-optimizer/src/optimizer/cache_keys.ts +++ b/packages/kbn-optimizer/src/optimizer/cache_keys.ts @@ -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], @@ -133,7 +129,16 @@ async function getLastCommit() { return stdout.trim() || undefined; } -export async function getOptimizerCacheKey(config: OptimizerConfig) { +export async function getOptimizerCacheKey(config: OptimizerConfig): Promise { + 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(), diff --git a/packages/kbn-optimizer/src/optimizer/get_changes.test.ts b/packages/kbn-optimizer/src/optimizer/get_changes.test.ts index d8be1917c101b..d3cc5cceefddf 100644 --- a/packages/kbn-optimizer/src/optimizer/get_changes.test.ts +++ b/packages/kbn-optimizer/src/optimizer/get_changes.test.ts @@ -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 { @@ -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", + /kbn-optimizer/package.json => "modified", + /kbn-optimizer/src/common/bundle.ts => "modified", + /kbn-optimizer/src/common/bundles.ts => "deleted", + /kbn-optimizer/src/get_bundle_definitions.test.ts => "deleted", } `); }); diff --git a/packages/kbn-optimizer/src/optimizer/get_changes.ts b/packages/kbn-optimizer/src/optimizer/get_changes.ts index a47e2e9caf48f..c5f8abe99c322 100644 --- a/packages/kbn-optimizer/src/optimizer/get_changes.ts +++ b/packages/kbn-optimizer/src/optimizer/get_changes.ts @@ -9,7 +9,6 @@ import Path from 'path'; import execa from 'execa'; -import fs from 'fs'; import { REPO_ROOT } from '@kbn/dev-utils'; @@ -21,10 +20,6 @@ export type Changes = Map; 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, });