Skip to content

Add overwrite input to optionally allow copying over existing dependency files from non-root directories #1149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
check failure fix
  • Loading branch information
aparnajyothi-y committed Jul 11, 2025
commit 10f71d669285f3e58c1827e79bde3c0b4f60771c
64 changes: 27 additions & 37 deletions __tests__/setup-python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ const mockedGetCacheDistributor = getCacheDistributor as jest.Mock;
describe('cacheDependencies', () => {
const mockRestoreCache = jest.fn();

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

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

mockedCore.getInput.mockReturnValue('nested/deps.lock');
process.env.GITHUB_ACTION_PATH = actionPath;
process.env.GITHUB_WORKSPACE = workspacePath;

mockedCore.getInput.mockReturnValue(relPath);
mockedCore.getBooleanInput.mockReturnValue(false);

mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache});
Expand All @@ -42,47 +49,34 @@ describe('cacheDependencies', () => {
});

it('copies the file if source exists and target does not', async () => {
mockedFsPromises.access.mockImplementation(async filePath => {
if (filePath === '/github/action/nested/deps.lock')
return Promise.resolve(); // source
throw new Error('target does not exist'); // target
mockedFsPromises.access.mockImplementation(async p => {
if (p === sourcePath) return Promise.resolve();
throw new Error('target does not exist');
});

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

const sourcePath = '/github/action/nested/deps.lock';
const targetPath = '/github/workspace/nested/deps.lock';

expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(
sourcePath,
targetPath
);
expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(sourcePath, targetPath);
expect(mockedCore.info).toHaveBeenCalledWith(
`Copied ${sourcePath} to ${targetPath}`
);
});

it('overwrites file if target exists and overwrite is true', async () => {
mockedCore.getBooleanInput.mockReturnValue(true);
mockedFsPromises.access.mockResolvedValue(); // both source and target exist
mockedFsPromises.access.mockResolvedValue(); // both exist

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

const sourcePath = '/github/action/nested/deps.lock';
const targetPath = '/github/workspace/nested/deps.lock';

expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(
sourcePath,
targetPath
);
expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(sourcePath, targetPath);
expect(mockedCore.info).toHaveBeenCalledWith(
`Overwrote ${sourcePath} to ${targetPath}`
);
});

it('skips copy if file exists and overwrite is false', async () => {
mockedCore.getBooleanInput.mockReturnValue(false);
mockedFsPromises.access.mockResolvedValue(); // both source and target exist
mockedFsPromises.access.mockResolvedValue(); // both exist

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

Expand All @@ -93,11 +87,9 @@ describe('cacheDependencies', () => {
});

it('logs warning if source file does not exist', async () => {
mockedFsPromises.access.mockImplementation(async filePath => {
if (filePath === '/github/action/nested/deps.lock') {
throw new Error('source not found');
}
return Promise.resolve(); // fallback for others
mockedFsPromises.access.mockImplementation(async p => {
if (p === sourcePath) throw new Error('not found');
return Promise.resolve();
});

await cacheDependencies('pip', '3.12');
Expand All @@ -109,12 +101,10 @@ describe('cacheDependencies', () => {
});

it('logs warning if copyFile fails', async () => {
mockedFsPromises.access.mockImplementation(async filePath => {
if (filePath === '/github/action/nested/deps.lock')
return Promise.resolve();
throw new Error('target does not exist');
mockedFsPromises.access.mockImplementation(async p => {
if (p === sourcePath) return Promise.resolve();
throw new Error('target missing');
});

mockedFsPromises.copyFile.mockRejectedValue(new Error('copy failed'));

await cacheDependencies('pip', '3.12');
Expand All @@ -135,17 +125,17 @@ describe('cacheDependencies', () => {

it('does not copy if source and target are the same path', async () => {
mockedCore.getInput.mockReturnValue('deps.lock');
process.env.GITHUB_ACTION_PATH = '/github/workspace';
process.env.GITHUB_WORKSPACE = '/github/workspace';
const samePath = path.resolve('/github/workspace', 'deps.lock');
process.env.GITHUB_ACTION_PATH = workspacePath;
process.env.GITHUB_WORKSPACE = workspacePath;

mockedFsPromises.access.mockResolvedValue();

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

const sourcePath = '/github/workspace/deps.lock';
expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockedCore.info).toHaveBeenCalledWith(
`Dependency file is already inside the workspace: ${sourcePath}`
`Dependency file is already inside the workspace: ${samePath}`
);
});
});
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy