Skip to content

Commit a8a0b27

Browse files
Fix datascience functional tests.
1 parent 6f5e27b commit a8a0b27

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

src/client/common/platform/fileSystem.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export class FileSystemUtils implements IFileSystemUtils {
323323
): Promise<boolean> {
324324
let stat: FileStat;
325325
try {
326-
stat = await this.raw.stat(filename);
326+
stat = await this._stat(filename);
327327
} catch (err) {
328328
return false;
329329
}
@@ -371,7 +371,7 @@ export class FileSystemUtils implements IFileSystemUtils {
371371
try {
372372
tmpFile = await this.tmp.createFile('___vscpTest___', dirname);
373373
} catch {
374-
await this.raw.stat(dirname); // fails if does not exist
374+
await this._stat(dirname); // fails if does not exist
375375
return true;
376376
}
377377
tmpFile.dispose();
@@ -395,6 +395,10 @@ export class FileSystemUtils implements IFileSystemUtils {
395395
});
396396
});
397397
}
398+
399+
protected async _stat(filename: string): Promise<FileStat> {
400+
return this.raw.stat(filename);
401+
}
398402
}
399403

400404
// more aliases (to cause less churn)
@@ -413,7 +417,7 @@ export class FileSystem extends FileSystemUtils implements IFileSystem {
413417
// aliases
414418

415419
public async stat(filePath: string): Promise<FileStat> {
416-
return this.raw.stat(filePath);
420+
return this._stat(filePath);
417421
}
418422

419423
public async readFile(filename: string): Promise<string> {

src/test/datascience/dataScienceIocContainer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
295295

296296
constructor() {
297297
super();
298+
this.vscode = false;
298299
const isRollingBuild = process.env ? process.env.VSCODE_PYTHON_ROLLING !== undefined : false;
299300
this.shouldMockJupyter = !isRollingBuild;
300301
this.asyncRegistry = new AsyncDisposableRegistry();

src/test/serviceRegistry.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import * as fsextra from 'fs-extra';
45
import { Container } from 'inversify';
6+
import * as path from 'path';
57
import { anything, instance, mock, when } from 'ts-mockito';
68
import * as TypeMoq from 'typemoq';
79
import { Disposable, Memento, OutputChannel } from 'vscode';
@@ -12,7 +14,7 @@ import { FileSystem } from '../client/common/platform/fileSystem';
1214
import { PathUtils } from '../client/common/platform/pathUtils';
1315
import { PlatformService } from '../client/common/platform/platformService';
1416
import { registerTypes as platformRegisterTypes } from '../client/common/platform/serviceRegistry';
15-
import { IFileSystem, IPlatformService } from '../client/common/platform/types';
17+
import { FileStat, FileType, IFileSystem, IPlatformService } from '../client/common/platform/types';
1618
import { BufferDecoder } from '../client/common/process/decoder';
1719
import { ProcessService } from '../client/common/process/proc';
1820
import { PythonExecutionFactory } from '../client/common/process/pythonExecutionFactory';
@@ -21,6 +23,7 @@ import { registerTypes as processRegisterTypes } from '../client/common/process/
2123
import { IBufferDecoder, IProcessServiceFactory, IPythonExecutionFactory, IPythonToolExecutionService } from '../client/common/process/types';
2224
import { registerTypes as commonRegisterTypes } from '../client/common/serviceRegistry';
2325
import { GLOBAL_MEMENTO, ICurrentProcess, IDisposableRegistry, ILogger, IMemento, IOutputChannel, IPathUtils, IsWindows, WORKSPACE_MEMENTO } from '../client/common/types';
26+
import { createDeferred } from '../client/common/utils/async';
2427
import { registerTypes as variableRegisterTypes } from '../client/common/variables/serviceRegistry';
2528
import { registerTypes as formattersRegisterTypes } from '../client/formatters/serviceRegistry';
2629
import { EnvironmentActivationService } from '../client/interpreter/activation/service';
@@ -43,7 +46,77 @@ import { MockMemento } from './mocks/mementos';
4346
import { MockProcessService } from './mocks/proc';
4447
import { MockProcess } from './mocks/process';
4548

49+
// This is necessary for unit tests and functional tests, since they
50+
// do not run under VS Code so they do not have access to the actual
51+
// "vscode" namespace.
52+
class LegacyFileSystem extends FileSystem {
53+
public async readFile(filename: string): Promise<string> {
54+
return fsextra.readFile(filename, 'utf8');
55+
}
56+
public async writeFile(filename: string, data: {}): Promise<void> {
57+
const options: fsextra.WriteFileOptions = {
58+
encoding: 'utf8'
59+
};
60+
return fsextra.writeFile(filename, data, options);
61+
}
62+
public async deleteDirectory(dirname: string): Promise<void> {
63+
return fsextra.stat(dirname)
64+
.then(() => fsextra.remove(dirname));
65+
}
66+
public async deleteFile(filename: string): Promise<void> {
67+
return fsextra.unlink(filename);
68+
}
69+
public async listdir(dirname: string): Promise<[string, FileType][]> {
70+
const names: string[] = await fsextra.readdir(dirname);
71+
const promises = names
72+
.map(name => {
73+
const filename = path.join(dirname, name);
74+
return this.raw.lstat(filename)
75+
.then(stat => [name, stat.type] as [string, FileType])
76+
.catch(() => [name, FileType.Unknown] as [string, FileType]);
77+
});
78+
return Promise.all(promises);
79+
}
80+
public async createDirectory(dirname: string): Promise<void> {
81+
return fsextra.mkdirp(dirname);
82+
}
83+
public async copyFile(src: string, dest: string): Promise<void> {
84+
const deferred = createDeferred<void>();
85+
const rs = fsextra.createReadStream(src)
86+
.on('error', (err) => {
87+
deferred.reject(err);
88+
});
89+
const ws = fsextra.createWriteStream(dest)
90+
.on('error', (err) => {
91+
deferred.reject(err);
92+
}).on('close', () => {
93+
deferred.resolve();
94+
});
95+
rs.pipe(ws);
96+
return deferred.promise;
97+
}
98+
protected async _stat(filePath: string): Promise<FileStat> {
99+
const stat = await fsextra.stat(filePath);
100+
let fileType = FileType.Unknown;
101+
if (stat.isFile()) {
102+
fileType = FileType.File;
103+
} else if (stat.isDirectory()) {
104+
fileType = FileType.Directory;
105+
} else if (stat.isSymbolicLink()) {
106+
fileType = FileType.SymbolicLink;
107+
}
108+
return {
109+
type: fileType,
110+
size: stat.size,
111+
ctime: stat.ctimeMs,
112+
mtime: stat.mtimeMs
113+
};
114+
}
115+
}
116+
46117
export class IocContainer {
118+
public vscode = true;
119+
47120
public readonly serviceManager: IServiceManager;
48121
public readonly serviceContainer: IServiceContainer;
49122

@@ -90,7 +163,10 @@ export class IocContainer {
90163
}
91164
public registerFileSystemTypes() {
92165
this.serviceManager.addSingleton<IPlatformService>(IPlatformService, PlatformService);
93-
this.serviceManager.addSingleton<IFileSystem>(IFileSystem, FileSystem);
166+
this.serviceManager.addSingleton<IFileSystem>(
167+
IFileSystem,
168+
this.vscode ? FileSystem : LegacyFileSystem
169+
);
94170
}
95171
public registerProcessTypes() {
96172
processRegisterTypes(this.serviceManager);

0 commit comments

Comments
 (0)