Skip to content
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
1 change: 1 addition & 0 deletions lib/constants/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const STORE_KEYS = {
analysisURL: 'analysisURL',

analysisWasStarted: 'analysisWasStarted',
testEnvironment: 'testEnvironment',
};

export const SHARED_STORE_KEYS = [
Expand Down
10 changes: 9 additions & 1 deletion lib/deepcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,15 @@ export default {
PluginManager.checkFilters(testCallback);
},

scanProject() {
async createBundle() {
return PluginManager.createBundle();
},

async createRemoteBundle() {
return PluginManager.createRemoteBundle();
},

async checkAnalysis() {
return PluginManager.checkAnalysis();
},
};
8 changes: 6 additions & 2 deletions lib/modules/BundleModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ class BundleModule {
const scanningInProcess = Store.get(STORE_KEYS.scanningInProcess);
const uploadInProgress = Store.get(STORE_KEYS.uploadInProgress);
const analysisInProgress = Store.get(STORE_KEYS.analysisInProgress);
const testingInProgress = Store.get(STORE_KEYS.testEnvironment);

const isBlocked = (scanningInProcess || uploadInProgress || analysisInProgress);
const isBlocked = (scanningInProcess || uploadInProgress || analysisInProgress || testingInProgress);

return Promise.resolve(isBlocked);
}
Expand Down Expand Up @@ -342,6 +343,7 @@ class BundleModule {

async createUploadQueue(chunks, uploadURL) {
const sessionToken = await this.getSessionToken();
const isTesting = Store.get(STORE_KEYS.testEnvironment);

const q = queue({
results: [],
Expand Down Expand Up @@ -369,7 +371,9 @@ class BundleModule {
};

q.push(async () => {
await CommonUtils.sleep(100);
if (!isTesting) {
await CommonUtils.sleep(100);
}
const { error, statusCode } = await HttpModule.uploadFiles(sessionToken, uploadURL, requestBody);
if (statusCode !== 200) {
debugInfo.errorText = BUNDLE_ERRORS.upload[statusCode] || error.message;
Expand Down
40 changes: 40 additions & 0 deletions lib/modules/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,46 @@ class PluginManager {
}, 0);
}

// Testing API ------------------------------------------------------------------------------------------------------

async scanConfirmedFolders() {
const confirmedFolders = Store.get(STORE_KEYS.confirmedFolders);
confirmedFolders.forEach(folder => {
FileWatcher.addFolderToScan(folder);
});

await FileWatcher.scanFolders();
return Promise.resolve();
}

async createBundle() {
await this.scanConfirmedFolders();
const bundle = await FileWatcher.buildBundle();

return bundle;
}

async createRemoteBundle() {
const sessionToken = Store.get(STORE_KEYS.sessionToken);

await this.scanConfirmedFolders();
await BundleModule.updateState();

const { files } = await FileWatcher.buildBundle();
const { bundleId } = await HttpModule.createBundle(sessionToken, { files });

const chunks = await BundleModule.createUploadChunks(files);

return { bundleId, chunks };
}

async checkAnalysis() {
const { analysisResults } = await Analyser.checkAnalysis();
const { origin, table } = await Analyser.adaptResults(analysisResults);

return Promise.resolve({ origin, table });
}

// Utils ------------------------------------------------------------------------------------------------------------

destroy() {
Expand Down
1 change: 1 addition & 0 deletions lib/modules/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const initState = {
[STORE_KEYS.analysisURL]: '',

[STORE_KEYS.analysisWasStarted]: false,
[STORE_KEYS.testEnvironment]: false,
};

class Store {
Expand Down
15 changes: 12 additions & 3 deletions lib/watchers/FileWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,12 @@ class FileWatcher {
}

async buildBundle(changedFiles = []) {
const isTesting = Store.get(STORE_KEYS.testEnvironment);

Store.set(STORE_KEYS.composingInProcess, true);
await CommonUtils.sleep(100);
if (!isTesting) {
await CommonUtils.sleep(100);
}

const sourceList = (isArray(changedFiles) && !isEmpty(changedFiles))
? changedFiles
Expand Down Expand Up @@ -239,11 +242,15 @@ class FileWatcher {
}

async scanFolders() {
const isTesting = Store.get(STORE_KEYS.testEnvironment);

Store.set(STORE_KEYS.scanningInProcess, true);
this.changedFiles = [];
this.removedFiles = [];

await CommonUtils.sleep(1000);
if (!isTesting) {
await CommonUtils.sleep(1000);
}

Logger.log(`Scanning folders started. Root folders to scan: ${this.foldersToScan.length}`);
this.foldersToScan.forEach(folder => {
Expand All @@ -267,7 +274,9 @@ class FileWatcher {

this.foldersToScan.forEach(folder => {
q.push(async () => {
await CommonUtils.sleep(100);
if (!isTesting) {
await CommonUtils.sleep(100);
}
await this.scanFolder(folder, q);
});
});
Expand Down
65 changes: 57 additions & 8 deletions spec/deepcode-spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use babel';

import { STORE_KEYS } from '../lib/constants/store';
import { mockState, startMockServer } from './mocks';
import { mockState, startMockServer, mockBundle, mockAnalysisResults, mockAnalysisTable } from './mocks';

startMockServer();

describe('Deepcode Plugin tests', () => {
let workspaceElement;
let activationPromise;
let dcPackage;
let timerCallback;

beforeEach(() => {
workspaceElement = atom.views.getView(atom.workspace);
Expand All @@ -24,9 +23,6 @@ describe('Deepcode Plugin tests', () => {

return Promise.resolve();
}

timerCallback = jasmine.createSpy('timerCallback');
jasmine.Clock.useMock();
});

describe('Pre-test configuring', () => {
Expand Down Expand Up @@ -65,7 +61,7 @@ describe('Deepcode Plugin tests', () => {
waitsForPromise(activationPromise);
});

it('fetched filters from server', () => {
it('fetches filters from server', () => {
dcPackage.setPluginState({
[STORE_KEYS.allowedFiles]: {},
});
Expand All @@ -75,6 +71,59 @@ describe('Deepcode Plugin tests', () => {
expect(result).toEqual(mockState[STORE_KEYS.allowedFiles]);
});
});
})
})
});
});

describe('Creating hashes bundle', () => {
beforeEach(() => {
waitsForPromise(activationPromise);
});

it('creates bundle', () => {
waitsForPromise(async () => {
const bundle = await dcPackage.createBundle();
expect(bundle).toEqual(mockBundle);
})
});
});

describe('Creating remote bundle', () => {
beforeEach(() => {
waitsForPromise(activationPromise);
});

it('creates bundle', () => {
dcPackage.setPluginState({
[STORE_KEYS.bundleID]: '',
});

waitsForPromise(async () => {
const { bundleId, chunks } = await dcPackage.createRemoteBundle();

expect(bundleId).toEqual(mockState[STORE_KEYS.bundleID]);
expect(chunks.length).toEqual(1);
expect(chunks[0].length).toEqual(4);
})
});
});

describe('Analyzing', () => {
beforeEach(() => {
waitsForPromise(activationPromise);
});

it('analyses bundle', () => {
dcPackage.setPluginState({
[STORE_KEYS.analysisResults]: { origin: {}, table: [] },
[STORE_KEYS.analysisURL]: '',
});

waitsForPromise(async () => {
const { origin, table } = await dcPackage.checkAnalysis();

expect(origin).toEqual(mockAnalysisResults);
expect(table).toEqual(mockAnalysisTable);
})
});
});
});
76 changes: 71 additions & 5 deletions spec/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,73 @@ import nock from 'nock';
import { STORE_KEYS } from '../lib/constants/store';
import { API } from '../lib/constants/api';

const mockProjectPath = path.resolve(__dirname, 'mocked_data');
const root = __dirname;
const mockProjectPath = path.resolve(root, 'mocked_data');
const analysedFile = `${mockProjectPath}/sample_repository/main.js`;
const bundleID = 'gh/deepcode/DEEPCODE_PRIVATE_BUNDLE/ee0745667800961b0f35d6a4cb4fb12f72373d641e75e464f6632813102afcf1';

export const mockBundle = {
files: {
[analysedFile]: '3e2979852cc2e97f48f7e7973a8b0837eb73ed0485c868176bc3aa58c499f534',
[`${mockProjectPath}/sample_repository/sub_folder/test2.js`]: 'c8bc645260a7d1a0d1349a72150cb65fa005188142dca30d09c3cc67c7974923',
[`${mockProjectPath}/sample_repository/utf8.js`]: 'cc2b67993e547813db67f57c6b20bff83bf4ade64ea2c3fb468d927425502804',
[`${mockProjectPath}/test.java`]: '09f4ca64118f029e5a894305dfc329c930ebd2a258052de9e81f895b055ec929',
}
}

export const mockAnalysisResults = {
files: {
[analysedFile]: { '0': [{ rows: [1, 2], cols: [3, 4], markers: [] }] },
},
suggestions: {
'0': {
id: 'TestSuggestion',
message: 'some message',
severity: 1,
}
},
};

export const mockAnalysisTable = [
{
fileName: '/home/incode/WIP/DeepCode/atom-plugin/spec/mocked_data/sample_repository/main.js',
localPath: '/home/incode/WIP/DeepCode/atom-plugin/spec/mocked_data/sample_repository/main.js',
localName: 'main.js',
message: 'some message',
position: '[1, 3]',
severity: 1,
startRow: 1,
startCol: 3,
endRow: 2,
endCol: 4,
markers: [],
suggestionID: 'TestSuggestion',
suggestionIndex: '0',
suggestionData: {
rows: [1, 2],
cols:[3, 4],
markers: [],
},
},
];

export const mockState = {
// shared
[STORE_KEYS.accountType]: 'free',
[STORE_KEYS.sessionToken]: '444641e35f09d515e366e88ffc0e5929ef42263505d3424184e94040ca7401bc',
[STORE_KEYS.serviceURL]: 'http://localhost:3000/',
[STORE_KEYS.serviceURL]: 'http://localhost:3000',

// project
[STORE_KEYS.confirmedFolders]: [mockProjectPath],
[STORE_KEYS.allowedFiles]: {
extensions: [".java", ".html", ".js", ".jsx", ".ts", ".tsx", ".vue", ".py"],
configFiles: [".pmdrc.xml", ".ruleset.xml", "ruleset.xml", ".eslintrc.js", ".eslintrc.json", ".eslintrc.yml", "tslint.json", ".pylintrc", "pylintrc"],
extensions: ['.java', '.html', '.js', '.jsx', '.ts', '.tsx', '.vue', '.py'],
configFiles: ['.pmdrc.xml', '.ruleset.xml', 'ruleset.xml', '.eslintrc.js', '.eslintrc.json', '.eslintrc.yml', 'tslint.json', '.pylintrc', 'pylintrc'],
},
[STORE_KEYS.bundleID]: 'gh/deepcode/DEEPCODE_PRIVATE_BUNDLE/ee0745667800961b0f35d6a4cb4fb12f72373d641e75e464f6632813102afcf1',
[STORE_KEYS.bundleID]: bundleID,
[STORE_KEYS.firstStart]: false,

// runtime
[STORE_KEYS.testEnvironment]: true,
};

export const startMockServer = () => {
Expand All @@ -36,4 +87,19 @@ export const startMockServer = () => {
});

mockedServer.get(API.filters).reply(200, mockState[STORE_KEYS.allowedFiles]);
mockedServer.post(API.createBundle).reply(200, {
statusCode: 200,
bundleId: bundleID,
});
mockedServer.post(`${API.createBundle}/${bundleID}`).reply(200, {
statusCode: 200,
bundleId: bundleID,
});
mockedServer.get(`${API.analysis(bundleID)}`).reply(200, {
statusCode: 200,
status: 'DONE',
progress: 1.0,
analysisResults: mockAnalysisResults,
analysisURL: 'test_analysis_url'
});
};