From 83cb6e51d73fc7aa1153a409a255310c3954e148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Fri, 14 May 2021 15:24:00 +0000 Subject: [PATCH] feat: move cache under `node_modules` --- lib/file-client/file-constants.ts | 24 +++++++++++- .../integration.action-exports.test.ts | 2 +- test/integration/integration.test.ts | 10 ++--- test/smoke/smoke.test.ts | 4 +- test/unit/file-client.test.ts | 39 +++++++++++++++++++ test/unit/repository-client.test.ts | 2 +- 6 files changed, 71 insertions(+), 10 deletions(-) diff --git a/lib/file-client/file-constants.ts b/lib/file-client/file-constants.ts index 9b9f0615..1ccd07c1 100644 --- a/lib/file-client/file-constants.ts +++ b/lib/file-client/file-constants.ts @@ -1,5 +1,27 @@ +import fs from 'fs'; + +const ROOT = '.'; +const CACHE_DIR = '/.cache-eslint-remote-tester'; +const NODE_MODULES = '/node_modules'; +const ESLINT_REMOTE_TESTER = '/eslint-remote-tester'; + +function initializeCacheDirectory() { + if (fs.existsSync(ROOT + NODE_MODULES + ESLINT_REMOTE_TESTER)) { + // Has eslint-remote-tester installed under node_modules + return ROOT + NODE_MODULES + ESLINT_REMOTE_TESTER + CACHE_DIR; + } + + if (fs.existsSync(ROOT + NODE_MODULES)) { + // Has node_modules but no eslint-remote-tester installed locally + return ROOT + NODE_MODULES + CACHE_DIR; + } + + // Has no node_modules + return ROOT + CACHE_DIR; +} + /** Directory where repositories are cloned to */ -export const CACHE_LOCATION = './.cache-eslint-remote-tester'; +export const CACHE_LOCATION = initializeCacheDirectory(); /** Directory where results are generated in to */ export const RESULTS_LOCATION = './eslint-remote-tester-results'; diff --git a/test/integration/integration.action-exports.test.ts b/test/integration/integration.action-exports.test.ts index 6b464644..57b0f049 100644 --- a/test/integration/integration.action-exports.test.ts +++ b/test/integration/integration.action-exports.test.ts @@ -29,7 +29,7 @@ describe('integration - compare action exports', () => { expect( actionExports.RESULTS_COMPARISON_CACHE_LOCATION ).toMatchInlineSnapshot( - `"./.cache-eslint-remote-tester/.comparison-cache.json"` + `"./node_modules/.cache-eslint-remote-tester/.comparison-cache.json"` ); }); diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index f59ab271..98f6e3df 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -208,7 +208,7 @@ describe('integration', () => { [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target crashed: unable-to-parse-rule-id [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target 5 errors [DONE] Finished scan of 1 repositories - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester " `); @@ -248,7 +248,7 @@ describe('integration', () => { [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target crashed: unable-to-parse-rule-id [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target 5 errors [DONE] Finished scan of 1 repositories - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester " `); @@ -257,11 +257,11 @@ describe('integration', () => { expect(cachedRun.output.pop()).toMatchInlineSnapshot(` "Full log: - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target crashed: unable-to-parse-rule-id [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target 5 errors [DONE] Finished scan of 1 repositories - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester " `); @@ -1092,7 +1092,7 @@ describe('integration', () => { eol-last - AriPerkkio/eslint-remote-tester-integration-test-target [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target 21 errors [DONE] Finished scan of 1 repositories - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester " `); diff --git a/test/smoke/smoke.test.ts b/test/smoke/smoke.test.ts index 8cb27c85..22913460 100644 --- a/test/smoke/smoke.test.ts +++ b/test/smoke/smoke.test.ts @@ -23,10 +23,10 @@ describe('smoke', () => { expect(output.pop()).toMatchInlineSnapshot(` "Full log: - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester [ERROR] AriPerkkio/eslint-remote-tester-integration-test-target 56000 errors [DONE] Finished scan of 1 repositories - [INFO] Cached repositories (1) at ./.cache-eslint-remote-tester + [INFO] Cached repositories (1) at ./node_modules/.cache-eslint-remote-tester [DONE] Result comparison: Added 56000. Removed 56000. " diff --git a/test/unit/file-client.test.ts b/test/unit/file-client.test.ts index d8c8d0b4..38eadb4a 100644 --- a/test/unit/file-client.test.ts +++ b/test/unit/file-client.test.ts @@ -97,6 +97,18 @@ function createComparisonResults(): void { ); } +function getCacheLocation() { + let location = ''; + + jest.resetModuleRegistry(); + jest.isolateModules(() => { + location = require('../../lib/file-client/file-constants') + .CACHE_LOCATION; + }); + + return location; +} + describe('file-client', () => { describe('prepareResultsDirectory', () => { afterEach(() => { @@ -244,4 +256,31 @@ describe('file-client', () => { ]); }); }); + + describe('CACHE_LOCATION', () => { + test('is initialized under package in node_modules', () => { + jest.mock('fs', () => ({ existsSync: () => true })); + + expect(getCacheLocation()).toBe( + './node_modules/eslint-remote-tester/.cache-eslint-remote-tester' + ); + }); + + test('is initialized under node_modules when package is not installed', () => { + jest.mock('fs', () => ({ + existsSync: (path: string) => + path !== './node_modules/eslint-remote-tester', + })); + + expect(getCacheLocation()).toBe( + './node_modules/.cache-eslint-remote-tester' + ); + }); + + test('is initialized in root when node_modules is not available', () => { + jest.mock('fs', () => ({ existsSync: () => false })); + + expect(getCacheLocation()).toBe('./.cache-eslint-remote-tester'); + }); + }); }); diff --git a/test/unit/repository-client.test.ts b/test/unit/repository-client.test.ts index e24bf0cd..1aa49253 100644 --- a/test/unit/repository-client.test.ts +++ b/test/unit/repository-client.test.ts @@ -7,7 +7,7 @@ import { } from '@file-client/repository-client'; import SimpleGit from '__mocks__/simple-git'; -const EXPECTED_CACHE = './.cache-eslint-remote-tester'; +const EXPECTED_CACHE = './node_modules/.cache-eslint-remote-tester'; describe('repository-client', () => { beforeEach(() => {