handle non-dir cache-dependency-path

This commit is contained in:
Sergey Dolin 2023-05-04 16:05:46 +02:00
parent 3527195992
commit 8f7008bbe6
5 changed files with 118 additions and 61 deletions

View File

@ -1,11 +1,10 @@
import os from 'os';
import * as fs from 'fs';
import fs from 'fs';
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;
@ -212,65 +211,96 @@ describe('authutil tests', () => {
);
});
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();
});
describe('getPackageManagerWorkingDir', () => {
let existsSpy: jest.SpyInstance;
let lstatSpy: jest.SpyInstance;
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();
});
beforeEach(() => {
existsSpy = jest.spyOn(fs, 'existsSync');
existsSpy.mockImplementation(() => true);
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();
});
lstatSpy = jest.spyOn(fs, 'lstatSync');
lstatSpy.mockImplementation(arg => ({
isDirectory: () => true
}));
});
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));
});
afterEach(() => {
existsSpy.mockRestore();
lstatSpy.mockRestore();
});
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;
const getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));
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();
});
const version = await cacheUtils.getPackageManagerVersion('foo', 'bar');
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`foo bar`,
path.dirname(cachePath)
);
});
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('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;
const getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));
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();
});
const version = await cacheUtils.getCacheDirectoryPath(
{lockFilePatterns: [], getCacheFolderCommand: 'quz'},
''
);
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`quz`,
path.dirname(cachePath)
);
it('getPackageManagerWorkingDir should return null for yarn with cache-dependency-path for not-existing directory', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
lstatSpy.mockImplementation(arg => ({
isDirectory: () => false
}));
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;
const 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;
const getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));
const version = await cacheUtils.getCacheDirectoryPath(
{lockFilePatterns: [], getCacheFolderCommand: 'quz'},
''
);
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`quz`,
path.dirname(cachePath)
);
});
});
});

View File

@ -59253,6 +59253,7 @@ const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147));
exports.supportedPackageManagers = {
npm: {
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
@ -59288,7 +59289,14 @@ const getPackageManagerWorkingDir = () => {
return null;
}
const cacheDependencyPath = core.getInput('cache-dependency-path');
return cacheDependencyPath ? path_1.default.dirname(cacheDependencyPath) : null;
if (!cacheDependencyPath) {
return null;
}
const wd = path_1.default.dirname(cacheDependencyPath);
if (fs_1.default.existsSync(wd) && fs_1.default.lstatSync(wd).isDirectory()) {
return wd;
}
return null;
};
exports.getPackageManagerWorkingDir = getPackageManagerWorkingDir;
const getPackageManagerCommandOutput = (command) => exports.getCommandOutput(command, exports.getPackageManagerWorkingDir());

12
dist/setup/index.js vendored
View File

@ -71167,7 +71167,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
exports.restoreCache = restoreCache;
const findLockFile = (packageManager) => {
const lockFiles = packageManager.lockFilePatterns;
const workspace = cache_utils_1.getPackageManagerWorkingDir() || process.env.GITHUB_WORKSPACE;
const workspace = process.env.GITHUB_WORKSPACE;
const rootContent = fs_1.default.readdirSync(workspace);
const lockFile = lockFiles.find(item => rootContent.includes(item));
if (!lockFile) {
@ -71221,6 +71221,7 @@ const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147));
exports.supportedPackageManagers = {
npm: {
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
@ -71256,7 +71257,14 @@ const getPackageManagerWorkingDir = () => {
return null;
}
const cacheDependencyPath = core.getInput('cache-dependency-path');
return cacheDependencyPath ? path_1.default.dirname(cacheDependencyPath) : null;
if (!cacheDependencyPath) {
return null;
}
const wd = path_1.default.dirname(cacheDependencyPath);
if (fs_1.default.existsSync(wd) && fs_1.default.lstatSync(wd).isDirectory()) {
return wd;
}
return null;
};
exports.getPackageManagerWorkingDir = getPackageManagerWorkingDir;
const getPackageManagerCommandOutput = (command) => exports.getCommandOutput(command, exports.getPackageManagerWorkingDir());

View File

@ -56,8 +56,8 @@ export const restoreCache = async (
const findLockFile = (packageManager: PackageManagerInfo) => {
const lockFiles = packageManager.lockFilePatterns;
const workspace =
getPackageManagerWorkingDir() || process.env.GITHUB_WORKSPACE!;
const workspace = process.env.GITHUB_WORKSPACE!;
const rootContent = fs.readdirSync(workspace);
const lockFile = lockFiles.find(item => rootContent.includes(item));

View File

@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import path from 'path';
import fs from 'fs';
type SupportedPackageManagers = {
[prop: string]: PackageManagerInfo;
@ -58,7 +59,17 @@ export const getPackageManagerWorkingDir = (): string | null => {
}
const cacheDependencyPath = core.getInput('cache-dependency-path');
return cacheDependencyPath ? path.dirname(cacheDependencyPath) : null;
if (!cacheDependencyPath) {
return null;
}
const wd = path.dirname(cacheDependencyPath);
if (fs.existsSync(wd) && fs.lstatSync(wd).isDirectory()) {
return wd;
}
return null;
};
export const getPackageManagerCommandOutput = (command: string) =>