Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jun 22, 2023
1 parent 1db1d57 commit 8d52008
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
6 changes: 1 addition & 5 deletions __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ describe('cache-restore', () => {
let getCommandOutputSpy: jest.SpyInstance;
let restoreCacheSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance;
let inSpy: jest.SpyInstance;

beforeEach(() => {
// core
Expand All @@ -70,8 +69,6 @@ describe('cache-restore', () => {
saveStateSpy.mockImplementation(() => undefined);

inputs = {};
inSpy = jest.spyOn(core, 'getInput');
inSpy.mockImplementation(name => inputs[name]);

// glob
hashFilesSpy = jest.spyOn(glob, 'hashFiles');
Expand Down Expand Up @@ -137,12 +134,11 @@ describe('cache-restore', () => {
return findCacheFolder(command);
}
});
inputs['node-version'] = 'vABC';

await restoreCache(packageManager, '');
expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: node-cache-${platform}-${packageManager}-v2-vABC-${fileHash}`
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`${packageManager} cache is not found`
Expand Down
7 changes: 3 additions & 4 deletions __tests__/cache-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
PackageManagerInfo,
isCacheFeatureAvailable,
supportedPackageManagers,
getCommandOutput
getCommandOutput,
resetProjectDirectoriesMemoized
} from '../src/cache-utils';
import fs from 'fs';
import * as cacheUtils from '../src/cache-utils';
Expand Down Expand Up @@ -104,9 +105,7 @@ describe('cache-utils', () => {
MockGlobber.create(['/foo', '/bar'])
);

Object.keys(expandedPatternsMemoized).forEach(
key => delete expandedPatternsMemoized[key]
);
resetProjectDirectoriesMemoized();
});

afterEach(() => {
Expand Down
7 changes: 6 additions & 1 deletion dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60434,7 +60434,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarn3ManagedCache = exports.getCacheDirectories = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarn3ManagedCache = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
Expand Down Expand Up @@ -60511,6 +60511,11 @@ exports.getPackageManagerInfo = getPackageManagerInfo;
* it contains expensive IO operation and thus should be memoized
*/
let projectDirectoriesMemoized = null;
/**
* unit test must reset memoized variables
*/
const resetProjectDirectoriesMemoized = () => (projectDirectoriesMemoized = null);
exports.resetProjectDirectoriesMemoized = resetProjectDirectoriesMemoized;
/**
* Expands (converts) the string input `cache-dependency-path` to list of directories that
* may be project roots
Expand Down
7 changes: 6 additions & 1 deletion dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71220,7 +71220,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarn3ManagedCache = exports.getCacheDirectories = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarn3ManagedCache = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
Expand Down Expand Up @@ -71297,6 +71297,11 @@ exports.getPackageManagerInfo = getPackageManagerInfo;
* it contains expensive IO operation and thus should be memoized
*/
let projectDirectoriesMemoized = null;
/**
* unit test must reset memoized variables
*/
const resetProjectDirectoriesMemoized = () => (projectDirectoriesMemoized = null);
exports.resetProjectDirectoriesMemoized = resetProjectDirectoriesMemoized;
/**
* Expands (converts) the string input `cache-dependency-path` to list of directories that
* may be project roots
Expand Down
5 changes: 5 additions & 0 deletions src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export const getPackageManagerInfo = async (packageManager: string) => {
*/

let projectDirectoriesMemoized: string[] | null = null;
/**
* unit test must reset memoized variables
*/
export const resetProjectDirectoriesMemoized = () =>
(projectDirectoriesMemoized = null);
/**
* Expands (converts) the string input `cache-dependency-path` to list of directories that
* may be project roots
Expand Down

0 comments on commit 8d52008

Please sign in to comment.