-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pacmak): python pack fails when installing 'black' via pip (#1782)
The failure is because of intermittent unavailability of black or one of its packages from the PyPI index. Instead, install back into the environment, i.e., superchain. Fallback to installing it once in the user's home directory.
- Loading branch information
Niranjan Jayakar
authored
Jul 10, 2020
1 parent
bdb5483
commit d83e004
Showing
5 changed files
with
165 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as util from '../../lib/util'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import Python from '../../lib/targets/python'; | ||
import { Assembly } from 'jsii-reflect'; | ||
import { Rosetta } from 'jsii-rosetta'; | ||
|
||
describe('python', () => { | ||
describe('blackPath', () => { | ||
const shellMock = jest.fn(); | ||
const homedirMock = jest.fn(); | ||
let homedir: string; | ||
let python: Python; | ||
|
||
beforeEach(async () => { | ||
// eslint-disable-next-line no-import-assign | ||
Object.defineProperty(util, 'shell', { value: shellMock }); | ||
// eslint-disable-next-line no-import-assign | ||
Object.defineProperty(os, 'homedir', { value: homedirMock }); | ||
homedir = await fs.mkdtemp(path.join(os.tmpdir(), 'jsii-pacmak-black-')); | ||
homedirMock.mockImplementation(() => homedir); | ||
python = new Python({ | ||
targetName: 'python', | ||
packageDir: '/dir', | ||
assembly: {} as Assembly, | ||
rosetta: new Rosetta(), | ||
arguments: {}, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
shellMock.mockClear(); | ||
homedirMock.mockClear(); | ||
await fs.remove(homedir); | ||
}); | ||
|
||
test('black is installed globally', async () => { | ||
let badShellCommand: string | undefined; | ||
shellMock.mockImplementation((cmd: string, args: string[], _) => { | ||
return new Promise((ok, ko) => { | ||
if (cmd === 'which' && args[0] === 'black') { | ||
ok('/path/to/black'); | ||
} else { | ||
badShellCommand = `Unexpected call to shell [${cmd} ${args}]`; | ||
ko(badShellCommand); | ||
} | ||
}); | ||
}); | ||
|
||
const path = await (python as any).blackPath(); // call private method blackPath() | ||
expect(badShellCommand).toBeUndefined(); | ||
expect(path).toBe('black'); | ||
}); | ||
|
||
test('black is installed if not found globally', async () => { | ||
shellMock.mockImplementation((cmd: string, args: string[], _) => { | ||
return new Promise((ok, ko) => { | ||
if (cmd === 'which' && args[0] === 'black') { | ||
ko('black not found'); | ||
} else if ( | ||
/pip.?$/.test(cmd) && | ||
args[0] === 'show' && | ||
args[1] === 'black' | ||
) { | ||
ko(); | ||
} else { | ||
ok(); | ||
} | ||
}); | ||
}); | ||
|
||
const path = await (python as any).blackPath(); // call private method blackPath() | ||
expect(path).toBe(`${homedir}/.jsii-cache/python-black/.env/bin/black`); | ||
}); | ||
|
||
test('local cache is reused', async () => { | ||
let installCount = 0; | ||
shellMock.mockImplementation((cmd: string, args: string[], _) => { | ||
return new Promise((ok, ko) => { | ||
if (cmd === 'which' && args[0] === 'black') { | ||
ko('black not found'); | ||
} else if ( | ||
/pip.?$/.test(cmd) && | ||
args[0] === 'show' && | ||
args[1] === 'black' | ||
) { | ||
ko(); | ||
} else if ( | ||
/pip.?$/.test(cmd) && | ||
args[0] === 'install' && | ||
args[1] === 'black' | ||
) { | ||
installCount++; | ||
ok(); | ||
} else { | ||
ok(); | ||
} | ||
}); | ||
}); | ||
|
||
await (python as any).blackPath(); | ||
await (python as any).blackPath(); | ||
await (python as any).blackPath(); | ||
expect(installCount).toEqual(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters