Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Apr 19, 2023
1 parent c1712a6 commit 3e5c14d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
64 changes: 64 additions & 0 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as auth from '../src/authutil';
import * as cacheUtils from '../src/cache-utils';
import {getCacheDirectoryPath} from '../src/cache-utils';

let rcFile: string;

Expand Down Expand Up @@ -209,4 +211,66 @@ describe('authutil tests', () => {
`@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
);
});

it('getPackageManagerWorkingDir should return null for not yarn', async () => {
process.env['INPUT_CACHE'] = 'some';
delete process.env['INPUT_CACHE-DEPENDENCY-PATH'];
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return null for not yarn with cache-dependency-path', async () => {
process.env['INPUT_CACHE'] = 'some';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = '/foo/bar';
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return null for yarn but without cache-dependency-path', async () => {
process.env['INPUT_CACHE'] = 'yarn';
delete process.env['INPUT_CACHE-DEPENDENCY-PATH'];
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return path for yarn with cache-dependency-path', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toEqual(path.dirname(cachePath));
});

it('getCommandOutput(getPackageManagerVersion) should be called from with getPackageManagerWorkingDir result', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
let getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));

const version = await cacheUtils.getPackageManagerVersion('foo', 'bar');
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`foo bar`,
path.dirname(cachePath)
);
});

it('getCommandOutput(getCacheDirectoryPath) should be called from with getPackageManagerWorkingDir result', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
let getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));

const version = await cacheUtils.getCacheDirectoryPath(
{lockFilePatterns: [], getCacheFolderCommand: 'quz'},
''
);
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`quz`,
path.dirname(cachePath)
);
});
});
5 changes: 3 additions & 2 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59628,7 +59628,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.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getPackageManagerWorkingDir = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getPackageManagerVersion = exports.getPackageManagerWorkingDir = 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 @@ -59678,6 +59678,7 @@ const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0,
}
return stdOut;
});
exports.getPackageManagerVersion = getPackageManagerVersion;
const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
if (packageManager === 'npm') {
return exports.supportedPackageManagers.npm;
Expand All @@ -59686,7 +59687,7 @@ const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void
return exports.supportedPackageManagers.pnpm;
}
else if (packageManager === 'yarn') {
const yarnVersion = yield getPackageManagerVersion('yarn', '--version');
const yarnVersion = yield exports.getPackageManagerVersion('yarn', '--version');
core.debug(`Consumed yarn version is ${yarnVersion}`);
if (yarnVersion.startsWith('1.')) {
return exports.supportedPackageManagers.yarn1;
Expand Down
5 changes: 3 additions & 2 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71596,7 +71596,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.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getPackageManagerWorkingDir = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getPackageManagerVersion = exports.getPackageManagerWorkingDir = 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 @@ -71646,6 +71646,7 @@ const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0,
}
return stdOut;
});
exports.getPackageManagerVersion = getPackageManagerVersion;
const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
if (packageManager === 'npm') {
return exports.supportedPackageManagers.npm;
Expand All @@ -71654,7 +71655,7 @@ const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void
return exports.supportedPackageManagers.pnpm;
}
else if (packageManager === 'yarn') {
const yarnVersion = yield getPackageManagerVersion('yarn', '--version');
const yarnVersion = yield exports.getPackageManagerVersion('yarn', '--version');
core.debug(`Consumed yarn version is ${yarnVersion}`);
if (yarnVersion.startsWith('1.')) {
return exports.supportedPackageManagers.yarn1;
Expand Down
2 changes: 1 addition & 1 deletion src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const getPackageManagerWorkingDir = (): string | null => {
return cacheDependencyPath ? path.dirname(cacheDependencyPath) : null;
};

const getPackageManagerVersion = async (
export const getPackageManagerVersion = async (
packageManager: string,
command: string
) => {
Expand Down

0 comments on commit 3e5c14d

Please sign in to comment.