Skip to content

Commit 84c4c8d

Browse files
author
Benjamin Pasero
committed
add meaningful defaults to paths, product and package to survive test runs
1 parent d2a413e commit 84c4c8d

8 files changed

Lines changed: 68 additions & 20 deletions

File tree

src/vs/base/node/paths.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,40 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import uri from 'vs/base/common/uri';
7+
import * as path from 'path';
8+
import * as os from 'os';
79

810
interface IPaths {
911
getAppDataPath(platform: string): string;
1012
getUserDataPath(platform: string, appName: string, args: string[]): string;
1113
}
1214

13-
const pathsPath = uri.parse(require.toUrl('paths')).fsPath;
14-
const paths = require.__$__nodeRequire<IPaths>(pathsPath);
15-
export const getAppDataPath = paths.getAppDataPath;
16-
export const getUserDataPath = paths.getUserDataPath;
15+
function defaultGetAppDataPath(platform) {
16+
switch (platform) {
17+
case 'win32': return process.env['APPDATA'];
18+
case 'darwin': return path.join(os.homedir(), 'Library', 'Application Support');
19+
case 'linux': return process.env['XDG_CONFIG_HOME'] || path.join(os.homedir(), '.config');
20+
default: throw new Error('Platform not supported');
21+
}
22+
}
23+
24+
function defaultGetUserDataPath(platform, appName) {
25+
return path.join(getAppDataPath(platform), appName);
26+
}
27+
28+
let _getAppDataPath: (platform: string) => string;
29+
let _getUserDataPath: (platform: string, appName: string, args: string[]) => string;
30+
31+
try {
32+
const pathsPath = uri.parse(require.toUrl('paths')).fsPath;
33+
const paths = require.__$__nodeRequire<IPaths>(pathsPath);
34+
35+
_getAppDataPath = paths.getAppDataPath;
36+
_getUserDataPath = paths.getUserDataPath;
37+
} catch (error) {
38+
_getAppDataPath = (platform) => defaultGetAppDataPath(platform);
39+
_getUserDataPath = (platform: string, appName: string, args: string[]) => defaultGetUserDataPath(platform, appName);
40+
}
41+
42+
export const getAppDataPath = _getAppDataPath;
43+
export const getUserDataPath = _getUserDataPath;

src/vs/platform/configuration/common/configurationService.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export abstract class ConfigurationService implements IConfigurationService, IDi
4848

4949
private _onDidUpdateConfiguration = new Emitter<IConfigurationServiceEvent>();
5050

51-
protected contextService: IWorkspaceContextService;
52-
protected eventService: IEventService;
5351
protected workspaceSettingsRootFolder: string;
5452

5553
private cachedConfig: ILoadConfigResult;
@@ -59,10 +57,11 @@ export abstract class ConfigurationService implements IConfigurationService, IDi
5957
private callOnDispose: IDisposable;
6058
private reloadConfigurationScheduler: RunOnceScheduler;
6159

62-
constructor(contextService: IWorkspaceContextService, eventService: IEventService, workspaceSettingsRootFolder: string = '.vscode') {
63-
this.contextService = contextService;
64-
this.eventService = eventService;
65-
60+
constructor(
61+
protected contextService: IWorkspaceContextService,
62+
protected eventService: IEventService,
63+
workspaceSettingsRootFolder: string = '.vscode'
64+
) {
6665
this.workspaceSettingsRootFolder = workspaceSettingsRootFolder;
6766
this.workspaceFilePathToConfiguration = Object.create(null);
6867
this.cachedConfig = {

src/vs/platform/package.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ export interface IPackageConfiguration {
1111
version: string;
1212
}
1313

14-
const rootPath = path.dirname(uri.parse(require.toUrl('')).fsPath);
15-
const packageJsonPath = path.join(rootPath, 'package.json');
16-
export default require.__$__nodeRequire(packageJsonPath) as IPackageConfiguration;
14+
let pkg: IPackageConfiguration;
15+
try {
16+
const rootPath = path.dirname(uri.parse(require.toUrl('')).fsPath);
17+
const packageJsonPath = path.join(rootPath, 'package.json');
18+
pkg = require.__$__nodeRequire(packageJsonPath) as IPackageConfiguration;
19+
} catch (error) {
20+
pkg = {
21+
name: 'code-oss-dev',
22+
version: '1.x.x'
23+
};
24+
}
25+
26+
export default pkg;

src/vs/platform/product.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ try {
5353
const productJsonPath = path.join(rootPath, 'product.json');
5454
product = require.__$__nodeRequire(productJsonPath) as IProductConfiguration;
5555
} catch (error) {
56-
product = Object.create(null); // can happen in environments where product.json is missing (e.g. when used from tests)
56+
product = <IProductConfiguration>{
57+
nameLong: 'Code - OSS',
58+
applicationName: 'code-oss',
59+
dataFolderName: '.vscode-oss'
60+
};
5761
}
5862

5963
if (process.env['VSCODE_DEV']) {

src/vs/test/utils/servicesTestUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import {IModelService} from 'vs/editor/common/services/modelService';
4141
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
4242
import {IRawTextContent} from 'vs/workbench/parts/files/common/files';
4343
import {RawText} from 'vs/editor/common/model/textModel';
44+
import {parseArgs} from 'vs/code/node/argv';
45+
import {EnvironmentService} from 'vs/platform/environment/node/environmentService';
4446

4547
export const TestWorkspace: IWorkspace = {
4648
resource: URI.file('C:\\testWorkspace'),
@@ -54,6 +56,8 @@ export const TestConfiguration: IConfiguration = {
5456
env: Object.create(null)
5557
};
5658

59+
export const TestEnvironmentService = new EnvironmentService(parseArgs(process.argv));
60+
5761
export class TestContextService implements WorkspaceContextService.IWorkspaceContextService {
5862
public _serviceBrand: any;
5963

src/vs/workbench/electron-browser/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function openWorkbench(args: ParsedArgs, workspace: IWorkspace, configuration: I
139139
const eventService = new EventService();
140140
const environmentService = new EnvironmentService(args);
141141
const contextService = new WorkspaceContextService(eventService, workspace, configuration, options);
142-
const configurationService = new ConfigurationService(contextService, eventService);
142+
const configurationService = new ConfigurationService(contextService, eventService, environmentService);
143143

144144
// Since the configuration service is one of the core services that is used in so many places, we initialize it
145145
// right before startup of the workbench shell to have its data ready for consumers

src/vs/workbench/services/configuration/node/configurationService.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {IConfigFile} from 'vs/platform/configuration/common/model';
1515
import objects = require('vs/base/common/objects');
1616
import {IStat, IContent, ConfigurationService as CommonConfigurationService} from 'vs/platform/configuration/common/configurationService';
1717
import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
18+
import {IEnvironmentService} from 'vs/platform/environment/common/environment';
1819
import {OptionsChangeEvent, EventType} from 'vs/workbench/common/events';
1920
import {IEventService} from 'vs/platform/event/common/event';
2021
import {IDisposable} from 'vs/base/common/lifecycle';
@@ -31,7 +32,8 @@ export class ConfigurationService extends CommonConfigurationService {
3132

3233
constructor(
3334
contextService: IWorkspaceContextService,
34-
eventService: IEventService
35+
eventService: IEventService,
36+
private environmentService: IEnvironmentService
3537
) {
3638
super(contextService, eventService);
3739

@@ -120,8 +122,8 @@ export class ConfigurationService extends CommonConfigurationService {
120122
}
121123

122124
public setUserConfiguration(key: any, value: any) : Thenable<void> {
123-
const appSettingsPath = this.contextService.getConfiguration().env.appSettingsPath;
124-
125+
const appSettingsPath = this.environmentService.appSettingsPath;
126+
125127
return readFile(appSettingsPath, 'utf8').then(content => {
126128
const {tabSize, insertSpaces} = this.getConfiguration<{ tabSize: number; insertSpaces: boolean }>('editor');
127129
const path: JSONPath = typeof key === 'string' ? (<string> key).split('.') : <JSONPath> key;

src/vs/workbench/test/browser/parts/quickOpen/quickopen.perf.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {QuickOpenHandler, IQuickOpenRegistry, Extensions} from 'vs/workbench/bro
2121
import {Registry} from 'vs/platform/platform';
2222
import {SearchService} from 'vs/workbench/services/search/node/searchService';
2323
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
24-
import {TestConfiguration, TestEditorService, TestEditorGroupService} from 'vs/test/utils/servicesTestUtils';
24+
import {TestEnvironmentService, TestConfiguration, TestEditorService, TestEditorGroupService} from 'vs/test/utils/servicesTestUtils';
25+
import {IEnvironmentService} from 'vs/platform/environment/common/environment';
2526
import * as Timer from 'vs/base/common/timer';
2627
import {TPromise} from 'vs/base/common/winjs.base';
2728
import URI from 'vs/base/common/uri';
@@ -51,13 +52,14 @@ suite('QuickOpen performance', () => {
5152
mtime: null
5253
}, TestConfiguration),
5354

54-
telemetryService: telemetryService
55+
telemetryService
5556
};
5657

5758
const services = ensureStaticPlatformServices(overrides);
5859
const instantiationService = services.instantiationService.createChild(new ServiceCollection(
5960
[IWorkbenchEditorService, new TestEditorService()],
6061
[IEditorGroupService, new TestEditorGroupService()],
62+
[IEnvironmentService, TestEnvironmentService],
6163
[IUntitledEditorService, createSyncDescriptor(UntitledEditorService)],
6264
[ISearchService, createSyncDescriptor(SearchService)]
6365
));

0 commit comments

Comments
 (0)