Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code]: more clean up to the test option #33355

Merged
merged 1 commit into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions x-pack/plugins/code/server/__tests__/clone_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,14 @@ import { Logger } from '../log';
import { CloneWorker } from '../queue';
import { IndexWorker } from '../queue';
import { RepositoryServiceFactory } from '../repository_service_factory';
import { ServerOptions } from '../server_options';
import { emptyAsyncFunc, TEST_OPTIONS } from '../test_utils';
import { createTestServerOption, emptyAsyncFunc } from '../test_utils';
import { ConsoleLoggerFactory } from '../utils/console_logger_factory';

const log: Logger = new ConsoleLoggerFactory().getLogger(['test']);

const esQueue = {};

const config = {
get(key: string) {
if (key === 'path.data') {
return '/tmp/test';
}
},
};

const serverOptions = new ServerOptions(TEST_OPTIONS, config);
const serverOptions = createTestServerOption();

function prepareProject(url: string, p: string) {
return new Promise(resolve => {
Expand Down
13 changes: 2 additions & 11 deletions x-pack/plugins/code/server/__tests__/git_operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import os from 'os';
import path from 'path';
import rimraf from 'rimraf';
import { getDefaultBranch, GitOperations } from '../git_operations';
import { ServerOptions } from "../server_options";
import * as mkdirp from "mkdirp";
import Git from '@elastic/nodegit';
import { TEST_OPTIONS } from '../test_utils';
import { createTestServerOption } from '../test_utils';

describe('git_operations', () => {
it('get default branch from a non master repo', async () => {
Expand Down Expand Up @@ -70,16 +69,8 @@ describe('git_operations', () => {
);
});
const repoUri = 'github.com/test/test_repo';
const tmpDataPath = fs.mkdtempSync(path.join(os.tmpdir(), 'code_test'));
const config = {
get(key: string) {
if (key === 'path.data') {
return tmpDataPath;
}
}
};

const serverOptions = new ServerOptions(TEST_OPTIONS, config);
const serverOptions = createTestServerOption();

it('can iterate a repo', async () => {
const g = new GitOperations(serverOptions.repoPath);
Expand Down
13 changes: 2 additions & 11 deletions x-pack/plugins/code/server/__tests__/lsp_indexer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { Logger } from '../log';
import { InstallManager } from '../lsp/install_manager';
import { LspService } from '../lsp/lsp_service';
import { RepositoryConfigController } from '../repository_config_controller';
import { ServerOptions } from '../server_options';
import { emptyAsyncFunc, TEST_OPTIONS } from '../test_utils';
import { createTestServerOption, emptyAsyncFunc } from '../test_utils';
import { ConsoleLoggerFactory } from '../utils/console_logger_factory';

const log: Logger = new ConsoleLoggerFactory().getLogger(['test']);
Expand Down Expand Up @@ -60,15 +59,7 @@ function prepareProject(url: string, p: string) {

const repoUri = 'github.com/Microsoft/TypeScript-Node-Starter';

const config = {
get(key: string) {
if (key === 'path.data') {
return '/tmp/test';
}
},
};

const serverOptions = new ServerOptions(TEST_OPTIONS, config);
const serverOptions = createTestServerOption();

function cleanWorkspace() {
return new Promise(resolve => {
Expand Down
16 changes: 2 additions & 14 deletions x-pack/plugins/code/server/__tests__/lsp_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import path from 'path';
import mkdirp from 'mkdirp';

import { LspService } from "../lsp/lsp_service";
import { ServerOptions } from "../server_options";
import { ConsoleLoggerFactory } from "../utils/console_logger_factory";
import { RepositoryGitStatusReservedField, RepositoryConfigReservedField } from '../indexer/schema';
import { InstallManager } from "../lsp/install_manager";
import * as os from "os";
import assert from 'assert';
import { RepositoryConfigController } from '../repository_config_controller';
import { createTestServerOption } from '../test_utils';
import { Server } from 'hapi';
import { TEST_OPTIONS } from '../test_utils';

const filename = 'hello.ts';
describe('lsp_service tests', () => {
Expand All @@ -41,17 +39,7 @@ describe('lsp_service tests', () => {
return repo;
}

const tmpDataPath = fs.mkdtempSync(path.join(os.tmpdir(), 'code_test'));
console.log(`tmp data path is ${tmpDataPath}`);
const config = {
get(key: string) {
if (key === 'path.data') {
return tmpDataPath;
}
}
};

const serverOptions = new ServerOptions(TEST_OPTIONS, config);
const serverOptions = createTestServerOption();
const installManager = new InstallManager(new Server(), serverOptions);

function mockEsClient(): any {
Expand Down
21 changes: 4 additions & 17 deletions x-pack/plugins/code/server/lsp/language_server_launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,23 @@
*/

import fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import rimraf from 'rimraf';
import { TEST_OPTIONS } from '../test_utils';
import { createTestServerOption } from '../test_utils';
import { ConsoleLoggerFactory } from '../utils/console_logger_factory';
import { TYPESCRIPT } from './language_servers';
import { TypescriptServerLauncher } from './ts_launcher';

jest.setTimeout(10000);
const tmpDataPath = fs.mkdtempSync(path.join(os.tmpdir(), 'code_test'));

// @ts-ignore
const options: ServerOptions = {
workspacePath: `${tmpDataPath}/workspace`,
jdtWorkspacePath: `${tmpDataPath}/jdt`,
lsp: TEST_OPTIONS.lsp,
security: TEST_OPTIONS.security,
};
const options: ServerOptions = createTestServerOption();

beforeAll(async () => {
if (!fs.existsSync(options.workspacePath)) {
fs.mkdirSync(options.workspacePath);
fs.mkdirSync(options.jdtWorkspacePath);
fs.mkdirSync(options.workspacePath, { recursive: true });
fs.mkdirSync(options.jdtWorkspacePath, { recursive: true });
}
});

afterAll(() => {
rimraf.sync(tmpDataPath);
});

function delay(seconds: number) {
return new Promise(resolve => {
setTimeout(() => resolve(), seconds * 1000);
Expand Down
22 changes: 21 additions & 1 deletion x-pack/plugins/code/server/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import fs from 'fs';
import * as os from 'os';
import path from 'path';

import { AnyObject } from './lib/esqueue';
import { ServerOptions } from './server_options';

// TODO migrate other duplicate classes, functions

export const emptyAsyncFunc = async (_: AnyObject): Promise<any> => {
Promise.resolve({});
};
export const TEST_OPTIONS = {

const TEST_OPTIONS = {
enabled: true,
queueIndex: '.code_internal-worker-queue',
queueTimeout: 60 * 60 * 1000, // 1 hour by default
Expand All @@ -31,3 +37,17 @@ export const TEST_OPTIONS = {
maxWorkspace: 5, // max workspace folder for each language server
disableScheduler: true, // Temp option to disable all schedulers.
};

export function createTestServerOption() {
const tmpDataPath = fs.mkdtempSync(path.join(os.tmpdir(), 'code_test'));

const config = {
get(key: string) {
if (key === 'path.data') {
return tmpDataPath;
}
},
};

return new ServerOptions(TEST_OPTIONS, config);
}