Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions __tests__/setup-python.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import * as core from '@actions/core';
import * as fs from 'fs';
import * as path from 'path';
import {cacheDependencies} from '../src/setup-python';
import {getCacheDistributor} from '../src/cache-distributions/cache-factory';

jest.mock('fs', () => {
const actualFs = jest.requireActual('fs');
return {
...actualFs,
promises: {
access: jest.fn(),
mkdir: jest.fn(),
copyFile: jest.fn(),
writeFile: jest.fn(),
appendFile: jest.fn()
}
};
});
jest.mock('@actions/core');
jest.mock('../src/cache-distributions/cache-factory');

const mockedFsPromises = fs.promises as jest.Mocked<typeof fs.promises>;
const mockedCore = core as jest.Mocked<typeof core>;
const mockedGetCacheDistributor = getCacheDistributor as jest.Mock;

describe('cacheDependencies', () => {
const mockRestoreCache = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
process.env.GITHUB_ACTION_PATH = '/github/action';
process.env.GITHUB_WORKSPACE = '/github/workspace';

mockedCore.getInput.mockReturnValue('nested/deps.lock');

// Simulate file exists by resolving access without error
mockedFsPromises.access.mockImplementation(async (p) => {
const pathStr = typeof p === 'string' ? p : p.toString();
if (pathStr === '/github/action/nested/deps.lock') {
return Promise.resolve();
}
// Simulate directory doesn't exist to test mkdir
if (pathStr === path.dirname('/github/workspace/nested/deps.lock')) {
return Promise.reject(new Error('no dir'));
}
return Promise.resolve();
});

// Simulate mkdir success
mockedFsPromises.mkdir.mockResolvedValue(undefined);

// Simulate copyFile success
mockedFsPromises.copyFile.mockResolvedValue(undefined);

mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache});
});

it('copies the dependency file and resolves the path with directory structure', async () => {
await cacheDependencies('pip', '3.12');

const sourcePath = path.resolve('/github/action', 'nested/deps.lock');
const targetPath = path.resolve('/github/workspace', 'nested/deps.lock');

expect(mockedFsPromises.access).toHaveBeenCalledWith(sourcePath, fs.constants.F_OK);
expect(mockedFsPromises.mkdir).toHaveBeenCalledWith(path.dirname(targetPath), {
recursive: true
});
expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(sourcePath, targetPath);
expect(mockedCore.info).toHaveBeenCalledWith(
`Copied ${sourcePath} to ${targetPath}`
);
expect(mockedCore.info).toHaveBeenCalledWith(
`Resolved cache-dependency-path: nested/deps.lock`
);
expect(mockRestoreCache).toHaveBeenCalled();
});

it('warns if the dependency file does not exist', async () => {
// Simulate file does not exist by rejecting access
mockedFsPromises.access.mockRejectedValue(new Error('file not found'));

await cacheDependencies('pip', '3.12');

expect(mockedCore.warning).toHaveBeenCalledWith(
expect.stringContaining('does not exist')
);
expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockRestoreCache).toHaveBeenCalled();
});

it('warns if file copy fails', async () => {
// Simulate copyFile failure
mockedFsPromises.copyFile.mockRejectedValue(new Error('copy failed'));

await cacheDependencies('pip', '3.12');

expect(mockedCore.warning).toHaveBeenCalledWith(
expect.stringContaining('Failed to copy file')
);
expect(mockRestoreCache).toHaveBeenCalled();
});

it('skips path logic if no input is provided', async () => {
mockedCore.getInput.mockReturnValue('');

await cacheDependencies('pip', '3.12');

expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockedCore.warning).not.toHaveBeenCalled();
expect(mockRestoreCache).toHaveBeenCalled();
});
});
32 changes: 32 additions & 0 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96905,6 +96905,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.cacheDependencies = void 0;
const core = __importStar(__nccwpck_require__(7484));
const finder = __importStar(__nccwpck_require__(6843));
const finderPyPy = __importStar(__nccwpck_require__(2625));
Expand All @@ -96923,10 +96924,41 @@ function isGraalPyVersion(versionSpec) {
function cacheDependencies(cache, pythonVersion) {
return __awaiter(this, void 0, void 0, function* () {
const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined;
let resolvedDependencyPath = undefined;
if (cacheDependencyPath) {
const actionPath = process.env.GITHUB_ACTION_PATH || '';
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
const sourcePath = path.resolve(actionPath, cacheDependencyPath);
const relativePath = path.relative(actionPath, sourcePath);
const targetPath = path.resolve(workspace, relativePath);
try {
const sourceExists = yield fs_1.default.promises
.access(sourcePath, fs_1.default.constants.F_OK)
.then(() => true)
.catch(() => false);
if (!sourceExists) {
core.warning(`The resolved cache-dependency-path does not exist: ${sourcePath}`);
}
else if (sourcePath !== targetPath) {
const targetDir = path.dirname(targetPath);
// Create target directory if it doesn't exist
yield fs_1.default.promises.mkdir(targetDir, { recursive: true });
// Copy file asynchronously
yield fs_1.default.promises.copyFile(sourcePath, targetPath);
core.info(`Copied ${sourcePath} to ${targetPath}`);
}
resolvedDependencyPath = path.relative(workspace, targetPath);
core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`);
}
catch (error) {
core.warning(`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}`);
}
}
const cacheDistributor = (0, cache_factory_1.getCacheDistributor)(cache, pythonVersion, cacheDependencyPath);
yield cacheDistributor.restoreCache();
});
}
exports.cacheDependencies = cacheDependencies;
function resolveVersionInputFromDefaultFile() {
const couples = [
['.python-version', utils_1.getVersionsInputFromPlainFile]
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ steps:
- run: pip install -e .
# Or pip install -e '.[test]' to install test dependencies
```

Note: cache-dependency-path supports files located outside the workspace root by copying them into the workspace to enable proper caching.
# Outputs and environment variables

## Outputs
Expand Down
43 changes: 41 additions & 2 deletions src/setup-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,49 @@ function isPyPyVersion(versionSpec: string) {
function isGraalPyVersion(versionSpec: string) {
return versionSpec.startsWith('graalpy');
}

async function cacheDependencies(cache: string, pythonVersion: string) {
export async function cacheDependencies(cache: string, pythonVersion: string) {
const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined;
let resolvedDependencyPath: string | undefined = undefined;

if (cacheDependencyPath) {
const actionPath = process.env.GITHUB_ACTION_PATH || '';
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();

const sourcePath = path.resolve(actionPath, cacheDependencyPath);
const relativePath = path.relative(actionPath, sourcePath);
const targetPath = path.resolve(workspace, relativePath);

try {
const sourceExists = await fs.promises
.access(sourcePath, fs.constants.F_OK)
.then(() => true)
.catch(() => false);

if (!sourceExists) {
core.warning(
`The resolved cache-dependency-path does not exist: ${sourcePath}`
);
} else if (sourcePath !== targetPath) {
const targetDir = path.dirname(targetPath);

// Create target directory if it doesn't exist
await fs.promises.mkdir(targetDir, {recursive: true});

// Copy file asynchronously
await fs.promises.copyFile(sourcePath, targetPath);
core.info(`Copied ${sourcePath} to ${targetPath}`);
}

resolvedDependencyPath = path.relative(workspace, targetPath);
core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`);
} catch (error) {
core.warning(
`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}`
);
}
}

const cacheDistributor = getCacheDistributor(
cache,
pythonVersion,
Expand Down
Loading