|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | import * as fs from 'fs-extra'; |
| 5 | +import { applyEdits, ModificationOptions, modify } from 'jsonc-parser'; |
5 | 6 | import * as path from 'path'; |
| 7 | +import { IS_CI_SERVER } from '../ciConstants'; |
6 | 8 | import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../constants'; |
7 | | -/** |
8 | | - * Modify package.json to ensure VSC Notebooks have been setup so tests can run. |
9 | | - * This is required because we modify package.json during runtime, hence we need to do the same thing for tests. |
10 | | - */ |
11 | 9 |
|
12 | | -const packageJsonFile = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'package.json'); |
13 | | -const content = JSON.parse(fs.readFileSync(packageJsonFile).toString()); |
| 10 | +const settingsFile = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src/test/datascience/.vscode/settings.json'); |
14 | 11 |
|
15 | | -// This code is temporary. |
16 | | -if ( |
17 | | - !content.enableProposedApi || |
18 | | - !Array.isArray(content.contributes.notebookOutputRenderer) || |
19 | | - !Array.isArray(content.contributes.notebookProvider) |
20 | | -) { |
21 | | - content.enableProposedApi = true; |
22 | | - content.contributes.notebookOutputRenderer = [ |
23 | | - { |
24 | | - viewType: 'jupyter-notebook-renderer', |
25 | | - displayName: 'Jupyter Notebook Renderer', |
26 | | - mimeTypes: [ |
27 | | - 'application/geo+json', |
28 | | - 'application/vdom.v1+json', |
29 | | - 'application/vnd.dataresource+json', |
30 | | - 'application/vnd.plotly.v1+json', |
31 | | - 'application/vnd.vega.v2+json', |
32 | | - 'application/vnd.vega.v3+json', |
33 | | - 'application/vnd.vega.v4+json', |
34 | | - 'application/vnd.vega.v5+json', |
35 | | - 'application/vnd.vegalite.v1+json', |
36 | | - 'application/vnd.vegalite.v2+json', |
37 | | - 'application/vnd.vegalite.v3+json', |
38 | | - 'application/vnd.vegalite.v4+json', |
39 | | - 'application/x-nteract-model-debug+json', |
40 | | - 'image/gif', |
41 | | - 'image/png', |
42 | | - 'image/jpeg', |
43 | | - 'text/latex', |
44 | | - 'text/vnd.plotly.v1+html' |
45 | | - ] |
46 | | - } |
47 | | - ]; |
48 | | - content.contributes.notebookProvider = [ |
49 | | - { |
50 | | - viewType: 'jupyter-notebook', |
51 | | - displayName: 'Jupyter Notebook', |
52 | | - selector: [ |
53 | | - { |
54 | | - filenamePattern: '*.ipynb' |
55 | | - } |
56 | | - ] |
57 | | - } |
58 | | - ]; |
| 12 | +function updateTestsForNativeNotebooks() { |
| 13 | + /** |
| 14 | + * Modify package.json to ensure VSC Notebooks have been setup so tests can run. |
| 15 | + * This is required because we modify package.json during runtime, hence we need to do the same thing for tests. |
| 16 | + */ |
| 17 | + const packageJsonFile = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'package.json'); |
| 18 | + const content = JSON.parse(fs.readFileSync(packageJsonFile).toString()); |
| 19 | + |
| 20 | + // This code is temporary. |
| 21 | + if ( |
| 22 | + !content.enableProposedApi || |
| 23 | + !Array.isArray(content.contributes.notebookOutputRenderer) || |
| 24 | + !Array.isArray(content.contributes.notebookProvider) |
| 25 | + ) { |
| 26 | + content.enableProposedApi = true; |
| 27 | + content.contributes.notebookProvider = [ |
| 28 | + { |
| 29 | + viewType: 'jupyter-notebook', |
| 30 | + displayName: 'Jupyter Notebook', |
| 31 | + selector: [ |
| 32 | + { |
| 33 | + filenamePattern: '*.ipynb' |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + // Update package.json to pick experiments from our custom settings.json file. |
| 41 | + content.contributes.configuration.properties['python.experiments.optInto'].scope = 'resource'; |
| 42 | + content.contributes.configuration.properties['python.logging.level'].scope = 'resource'; |
| 43 | + |
| 44 | + fs.writeFileSync(packageJsonFile, JSON.stringify(content, undefined, 4)); |
| 45 | + |
| 46 | + updateSettings(true); |
59 | 47 | } |
60 | 48 |
|
61 | | -// Update package.json to pick experiments from our custom settings.json file. |
62 | | -content.contributes.configuration.properties['python.experiments.optInto'].scope = 'resource'; |
63 | | -content.contributes.configuration.properties['python.logging.level'].scope = 'resource'; |
| 49 | +function updateSettings(useNativeNotebooks: boolean) { |
| 50 | + const modificationOptions: ModificationOptions = { |
| 51 | + formattingOptions: { |
| 52 | + tabSize: 4, |
| 53 | + insertSpaces: true |
| 54 | + } |
| 55 | + }; |
| 56 | + let settingsJson = fs.readFileSync(settingsFile).toString(); |
| 57 | + const experiments = useNativeNotebooks ? ['NativeNotebook - experiment'] : []; |
| 58 | + const autoSave = useNativeNotebooks ? 'off' : 'afterDelay'; |
| 59 | + |
| 60 | + settingsJson = applyEdits( |
| 61 | + settingsJson, |
| 62 | + modify(settingsJson, ['python.experiments.optInto'], experiments, modificationOptions) |
| 63 | + ); |
| 64 | + settingsJson = applyEdits(settingsJson, modify(settingsJson, ['files.autoSave'], autoSave, modificationOptions)); |
| 65 | + |
| 66 | + fs.writeFileSync(settingsFile, settingsJson); |
| 67 | +} |
| 68 | +function updateTestsForOldNotebooks() { |
| 69 | + updateSettings(false); |
| 70 | +} |
64 | 71 |
|
65 | | -fs.writeFileSync(packageJsonFile, JSON.stringify(content, undefined, 4)); |
| 72 | +if (!IS_CI_SERVER) { |
| 73 | + // Noop. |
| 74 | +} else if (process.env.VSC_PYTHON_CI_TEST_VSC_CHANNEL === 'insiders') { |
| 75 | + updateTestsForNativeNotebooks(); |
| 76 | +} else { |
| 77 | + updateTestsForOldNotebooks(); |
| 78 | +} |
0 commit comments