Skip to content

Commit

Permalink
server tests and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Jul 31, 2023
1 parent 71239b8 commit f4db306
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ suite('pytest test execution adapter', () => {
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred2.resolve();
return Promise.resolve(execService.object);
});
utilsStub.callsFake(() => {
console.log('hi');
deferred3.resolve();
return Promise.resolve(54321);
});
Expand Down Expand Up @@ -142,12 +140,10 @@ suite('pytest test execution adapter', () => {
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred2.resolve();
return Promise.resolve(execService.object);
});
utilsStub.callsFake(() => {
console.log('hi');
deferred3.resolve();
return Promise.resolve(54321);
});
Expand Down Expand Up @@ -204,12 +200,10 @@ suite('pytest test execution adapter', () => {
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred2.resolve();
return Promise.resolve(execService.object);
});
utilsStub.callsFake(() => {
console.log('hi');
deferred3.resolve();
return Promise.resolve(54321);
});
Expand Down Expand Up @@ -269,7 +263,6 @@ suite('pytest test execution adapter', () => {
test('Debug launched correctly for pytest', async () => {
const deferred3 = createDeferred();
utilsStub.callsFake(() => {
console.log('hi');
deferred3.resolve();
return Promise.resolve(54321);
});
Expand Down
186 changes: 119 additions & 67 deletions src/test/testing/testController/server.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ suite('Python Test Server', () => {
cwd: '/foo/bar',
uuid: fakeUuid,
};
console.log('hello', execArgs, spawnOptions);
const expectedSpawnOptions = {
cwd: '/foo/bar',
outputChannel: undefined,
Expand All @@ -103,7 +102,6 @@ suite('Python Test Server', () => {
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred2.resolve();
return Promise.resolve(execService.object);
});
Expand Down Expand Up @@ -143,7 +141,6 @@ suite('Python Test Server', () => {
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});
Expand All @@ -163,13 +160,12 @@ suite('Python Test Server', () => {
});

test('If script execution fails during sendCommand, an onDataReceived event should be fired with the "error" status', async () => {
let eventData: { status: string; errors: string[] };
let eventData: { status: string; errors: string[] } | undefined;
stubExecutionService = ({
execObservable: () => {
throw new Error('Failed to execute');
},
} as unknown) as IPythonExecutionService;
// console.log('ugh', stubExecutionService);
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
Expand All @@ -186,45 +182,50 @@ suite('Python Test Server', () => {

await server.sendCommand(options);

assert.deepStrictEqual(eventData!.status, 'error');
assert.deepStrictEqual(eventData!.errors, ['Failed to execute']);
assert.notEqual(eventData, undefined);
assert.deepStrictEqual(eventData?.status, 'error');
assert.deepStrictEqual(eventData?.errors, ['Failed to execute']);
});

test('If the server receives malformed data, it should display a log message, and not fire an event', async () => {
let eventData: string | undefined;
const client = new net.Socket();

deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: fakeUuid,
};
mockProc = new MockChildProcess('', ['']);
const output = new Observable<Output<string>>(() => {
/* no op */
});
const stubExecutionService2 = ({
execObservable: (args: string[], spawnOptionsProvided: SpawnOptions) => {
client.connect(server.getPort());
execArgs = args;
spawnOptions = spawnOptionsProvided;
return ({
proc: mockProc,
out: output,
dispose: () => {
/* no-body */
},
})
},
} as unknown) as IPythonExecutionService;

server = new PythonTestServer(execFactory.object, debugLauncher);
const stubExecutionFactory2 = ({
createActivatedEnvironment: () => Promise.resolve(stubExecutionService2),
} as unknown) as IPythonExecutionFactory;

deferred = createDeferred();
server = new PythonTestServer(stubExecutionFactory2, debugLauncher);
await server.serverReady();
server.onDataReceived(({ data }) => {
eventData = data;
deferred.resolve();
});
deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});

client.on('connect', () => {
console.log('Socket connected, local port:', client.localPort);
Expand All @@ -240,40 +241,43 @@ suite('Python Test Server', () => {
await deferred.promise;
mockProc.trigger('close');

await deferred.promise;
assert.deepStrictEqual(eventData, '');
});

test('If the server doesnt recognize the UUID it should ignore it', async () => {
let eventData: string | undefined;
const client = new net.Socket();

deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: fakeUuid,
};

deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});
mockProc = new MockChildProcess('', ['']);
const output = new Observable<Output<string>>(() => {
/* no op */
});
const stubExecutionService2 = ({
execObservable: (args: string[], spawnOptionsProvided: SpawnOptions) => {
client.connect(server.getPort());
execArgs = args;
spawnOptions = spawnOptionsProvided;
return ({
proc: mockProc,
out: output,
dispose: () => {
/* no-body */
},
})
},
} as unknown) as IPythonExecutionService;

server = new PythonTestServer(execFactory.object, debugLauncher);
const stubExecutionFactory2 = ({
createActivatedEnvironment: () => Promise.resolve(stubExecutionService2),
} as unknown) as IPythonExecutionFactory;
server = new PythonTestServer(stubExecutionFactory2, debugLauncher);
await server.serverReady();
server.onDataReceived(({ data }) => {
eventData = data;
Expand Down Expand Up @@ -307,16 +311,29 @@ suite('Python Test Server', () => {
uuid: fakeUuid,
};
deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});
mockProc = new MockChildProcess('', ['']);
const output = new Observable<Output<string>>(() => {
/* no op */
});
const stubExecutionService2 = ({
execObservable: (args: string[], spawnOptionsProvided: SpawnOptions) => {
client.connect(server.getPort());
execArgs = args;
spawnOptions = spawnOptionsProvided;
return ({
proc: mockProc,
out: output,
dispose: () => {
/* no-body */
},
})
},
} as unknown) as IPythonExecutionService;

server = new PythonTestServer(execFactory.object, debugLauncher);
const stubExecutionFactory2 = ({
createActivatedEnvironment: () => Promise.resolve(stubExecutionService2),
} as unknown) as IPythonExecutionFactory;
server = new PythonTestServer(stubExecutionFactory2, debugLauncher);
await server.serverReady();
server.onDataReceived(({ data }) => {
eventData = data;
Expand Down Expand Up @@ -362,8 +379,30 @@ Request-uuid: UUID_HERE
cwd: '/foo/bar',
uuid: fakeUuid,
};
deferred = createDeferred();
mockProc = new MockChildProcess('', ['']);
const output = new Observable<Output<string>>(() => {
/* no op */
});
const stubExecutionService2 = ({
execObservable: (args: string[], spawnOptionsProvided: SpawnOptions) => {
client.connect(server.getPort());
execArgs = args;
spawnOptions = spawnOptionsProvided;
return ({
proc: mockProc,
out: output,
dispose: () => {
/* no-body */
},
})
},
} as unknown) as IPythonExecutionService;
const stubExecutionFactory2 = ({
createActivatedEnvironment: () => Promise.resolve(stubExecutionService2),
} as unknown) as IPythonExecutionFactory;

server = new PythonTestServer(execFactory.object, debugLauncher);
server = new PythonTestServer(stubExecutionFactory2, debugLauncher);
await server.serverReady();
const uuid = server.createUUID();
payload = payload.replace('UUID_HERE', uuid);
Expand Down Expand Up @@ -392,22 +431,36 @@ Request-uuid: UUID_HERE
const client = new net.Socket();

deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
console.log('hello');
deferred.resolve();
return Promise.resolve(execService.object);
});
mockProc = new MockChildProcess('', ['']);
const output = new Observable<Output<string>>(() => {
/* no op */
});
const stubExecutionService2 = ({
execObservable: (args: string[], spawnOptionsProvided: SpawnOptions) => {
client.connect(server.getPort());
execArgs = args;
spawnOptions = spawnOptionsProvided;
return ({
proc: mockProc,
out: output,
dispose: () => {
/* no-body */
},
})
},
} as unknown) as IPythonExecutionService;

const stubExecutionFactory2 = ({
createActivatedEnvironment: () => Promise.resolve(stubExecutionService2),
} as unknown) as IPythonExecutionFactory;
server = new PythonTestServer(stubExecutionFactory2, debugLauncher);
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: fakeUuid,
};

server = new PythonTestServer(execFactory.object, debugLauncher);
await server.serverReady();
const uuid = server.createUUID();
server.onRunDataReceived(({ data }) => {
Expand All @@ -432,7 +485,6 @@ Request-uuid: ${uuid}

server.sendCommand(options);
await deferred.promise;
console.log('event data', eventData);
const expectedResult =
'{"cwd": "path", "status": "success", "result": "xyz", "not_found": null, "error": null}';
assert.deepStrictEqual(eventData, expectedResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ suite('Workspace test adapter', () => {
const buildErrorNodeOptionsStub = sinon.stub(util, 'buildErrorNodeOptions').returns(errorTestItemOptions);
const testProvider = 'unittest';

const abc = await workspaceTestAdapter.discoverTests(testController);
console.log(abc);
await workspaceTestAdapter.discoverTests(testController);

sinon.assert.calledWithMatch(createErrorTestItemStub, sinon.match.any, sinon.match.any);
sinon.assert.calledWithMatch(buildErrorNodeOptionsStub, Uri.parse('foo'), sinon.match.any, testProvider);
Expand Down

0 comments on commit f4db306

Please sign in to comment.