Skip to content

Commit

Permalink
Ensure pip is initialized after install
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoH2O1999 committed Jul 3, 2023
1 parent bd1a153 commit a029fb1
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/__tests__/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ exports[`main Build behaior Allow leads to building from source with a message i
[
"Post-install operations done",
],
[
"Initializing pip...",
],
[
"Pip initialized",
],
]
`;

Expand Down Expand Up @@ -147,6 +153,12 @@ exports[`main Build behaior Info leads to building from source with a message in
[
"Post-install operations done",
],
[
"Initializing pip...",
],
[
"Pip initialized",
],
]
`;

Expand All @@ -169,6 +181,15 @@ Failure in build",
]
`;

exports[`main Builder handles failure in initPip 1`] = `
[
[
"Error during pip initialization.
Failure in initPip",
],
]
`;

exports[`main Builder handles failure in postInstall 1`] = `
[
[
Expand Down
80 changes: 77 additions & 3 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class MockBuilder extends builder.Builder {
protected os(): OS {
return 'linux';
}
override async initPip(): Promise<void> {}
}

describe('main', () => {
Expand Down Expand Up @@ -596,7 +597,7 @@ describe('main', () => {
});

describe('Builder', () => {
test('cache false leads to calling build, clean and postInstall on the builder', async () => {
test('cache false leads to calling build, clean, initPip and postInstall on the builder', async () => {
mockedInputs.parseInputs.mockResolvedValueOnce({
allowPrereleases: false,
architecture: 'x64',
Expand All @@ -620,6 +621,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -638,9 +640,11 @@ describe('main', () => {
'3.6.15',
'x64'
);
expect(mockInstanceInitPip).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledWith('install/path');
});

test('cache-hit leads to calling restoreCache, postInstall and clean on the builder', async () => {
test('cache-hit leads to calling restoreCache, postInstall, initPip and clean on the builder', async () => {
mockedInputs.parseInputs.mockResolvedValueOnce({
allowPrereleases: false,
architecture: 'x64',
Expand All @@ -664,6 +668,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -682,9 +687,11 @@ describe('main', () => {
'3.6.15',
'x64'
);
expect(mockInstanceInitPip).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledWith('install/path');
});

test('cache-miss leads to calling restoreCache, build, saveCache, postInstall and clean on the builder', async () => {
test('cache-miss leads to calling restoreCache, build, saveCache, postInstall, initPip and clean on the builder', async () => {
mockedInputs.parseInputs.mockResolvedValueOnce({
allowPrereleases: false,
architecture: 'x64',
Expand All @@ -711,6 +718,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -729,6 +737,8 @@ describe('main', () => {
'3.6.15',
'x64'
);
expect(mockInstanceInitPip).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledWith('install/path');
});

test('handles failure in restoreCache', async () => {
Expand Down Expand Up @@ -758,6 +768,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -770,6 +781,7 @@ describe('main', () => {
expect(mockedTc.cacheDir).not.toBeCalled();
expect(mockInstanceRestoreCache).toBeCalledTimes(1);
expect(mockedCore.setFailed.mock.calls).toMatchSnapshot();
expect(mockInstanceInitPip).not.toBeCalled();
});

test('handles failure in build', async () => {
Expand Down Expand Up @@ -802,6 +814,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -814,6 +827,7 @@ describe('main', () => {
expect(mockedTc.cacheDir).not.toBeCalled();
expect(mockInstanceRestoreCache).toBeCalledTimes(1);
expect(mockedCore.setFailed.mock.calls).toMatchSnapshot();
expect(mockInstanceInitPip).not.toBeCalled();
});

test('handles failure in saveCache', async () => {
Expand Down Expand Up @@ -846,6 +860,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -858,6 +873,7 @@ describe('main', () => {
expect(mockedTc.cacheDir).not.toBeCalled();
expect(mockInstanceRestoreCache).toBeCalledTimes(1);
expect(mockedCore.setFailed.mock.calls).toMatchSnapshot();
expect(mockInstanceInitPip).not.toBeCalled();
});

test('handles failure in postInstall', async () => {
Expand Down Expand Up @@ -890,6 +906,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -908,6 +925,60 @@ describe('main', () => {
);
expect(mockInstanceRestoreCache).toBeCalledTimes(1);
expect(mockedCore.setFailed.mock.calls).toMatchSnapshot();
expect(mockInstanceInitPip).not.toBeCalled();
});

test('handles failure in initPip', async () => {
mockedInputs.parseInputs.mockResolvedValueOnce({
allowPrereleases: false,
architecture: 'x64',
buildBehavior: inputs.BuildBehavior.Info,
cache: true,
checkLatest: false,
token: 'token',
version: {type: inputs.PythonType.CPython, version: '3.6.x'}
});
mockedVersion.getSetupPythonResult.mockResolvedValueOnce({
success: false,
version: '3.6.15'
});
mockedVersion.isPyPy.mockReturnValueOnce(false);
const instance = new MockBuilder(
{version: '3.6.15', zipBall: 'zipballUri'},
'x64'
);
instance.initPip = async () => {
throw new Error('Failure in initPip');
};
instance.restoreCache = async () => {
return null;
};
const mockInstanceBuild = jest.spyOn(instance, 'build');
const mockInstancePostInstall = jest.spyOn(instance, 'postInstall');
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

await main();

expect(mockInstanceBuild).toBeCalledTimes(1);
expect(mockInstancePostInstall).toBeCalledTimes(1);
expect(mockInstanceClean).not.toBeCalled();
expect(mockInstanceSaveCache).toBeCalledTimes(1);
expect(mockedTc.cacheDir).toBeCalledTimes(1);
expect(mockedTc.cacheDir).toBeCalledWith(
'build/path',
'Python',
'3.6.15',
'x64'
);
expect(mockInstanceRestoreCache).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledWith('install/path');
expect(mockedCore.setFailed.mock.calls).toMatchSnapshot();
});

test('fails to handle failure in clean', async () => {
Expand Down Expand Up @@ -940,6 +1011,7 @@ describe('main', () => {
const mockInstanceClean = jest.spyOn(instance, 'clean');
const mockInstanceSaveCache = jest.spyOn(instance, 'saveCache');
const mockInstanceRestoreCache = jest.spyOn(instance, 'restoreCache');
const mockInstanceInitPip = jest.spyOn(instance, 'initPip');
mockedBuilder.getBuilder.mockResolvedValueOnce(instance);
mockedTc.cacheDir.mockResolvedValueOnce('install/path');

Expand All @@ -956,6 +1028,8 @@ describe('main', () => {
'x64'
);
expect(mockInstanceRestoreCache).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledTimes(1);
expect(mockInstanceInitPip).toBeCalledWith('install/path');
expect(mockedCore.setFailed).not.toBeCalled();
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/builder/__tests__/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ exports[`class Builder clean method throws an error if path does not exist 1`] =

exports[`class Builder clean method throws an error if path is not defined 1`] = `"Base dir is not set."`;

exports[`class Builder initPip method Fails if get_pip.py fails and Python version >= 3.2 1`] = `"get_pip.py failed"`;

exports[`class Builder prepareSources method throws an error if the extracted folder does not have a subfolder starting with "python-cpython" 1`] = `"Expected directory to start with "python-cpython...", got otherFolder"`;

exports[`class Builder prepareSources method throws an error if the extracted folder has more than 1 subfolders 1`] = `"Expected only one folder. Got otherFolder,python-cpython"`;
Loading

0 comments on commit a029fb1

Please sign in to comment.