Skip to content

Commit

Permalink
Merge pull request #26 from snyk-tech-services/feat/delete-logs-helper
Browse files Browse the repository at this point in the history
test: delete test logs
  • Loading branch information
lili2311 authored Jun 3, 2020
2 parents 1e68a68 + 888d102 commit b019998
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 73 deletions.
11 changes: 11 additions & 0 deletions test/delete-logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as fs from 'fs';

export function deleteLogs(logs: string[]): void {
logs.forEach((path) => {
try {
fs.unlinkSync(path);
} catch (e) {
// do nothing
}
});
}
9 changes: 9 additions & 0 deletions test/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import {
} from '../../src/lib';
import { Project } from '../../src/lib/types';
import { deleteTestProjects } from '../delete-test-projects';
import { generateLogsPaths } from '../generate-log-file-names';
import { deleteLogs } from '../delete-logs';

const ORG_ID = 'f0125d9b-271a-4b50-ad23-80e12575a1bf';
const GITHUB_INTEGRATION_ID = 'c4de291b-e083-4c43-a72c-113463e0d268';

describe('Single target', () => {
const discoveredProjects: Project[] = [];
let logs: string[];
it('Import & poll a repo', async () => {
logs = Object.values(generateLogsPaths(__dirname));
const { pollingUrl } = await importTarget(ORG_ID, GITHUB_INTEGRATION_ID, {
name: 'shallow-goof-policy',
owner: 'snyk-fixtures',
Expand All @@ -29,12 +34,15 @@ describe('Single target', () => {
}, 30000000);
afterAll(async () => {
await deleteTestProjects(ORG_ID, discoveredProjects);
await deleteLogs(logs)
});
});

describe('Multiple targets', () => {
const discoveredProjects: Project[] = [];
let logs: string[];
it('importTargets & pollImportUrls multiple repos', async () => {
logs = Object.values(generateLogsPaths(__dirname));
const pollingUrls = await importTargets([
{
orgId: ORG_ID,
Expand Down Expand Up @@ -77,6 +85,7 @@ describe('Multiple targets', () => {
}, 30000000);
afterAll(async () => {
await deleteTestProjects(ORG_ID, discoveredProjects);
await deleteLogs(logs)
});
});

Expand Down
113 changes: 40 additions & 73 deletions test/scripts/import-projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ import * as path from 'path';
import * as fs from 'fs';

import { ImportProjects } from '../../src/scripts/import-projects';
import {
IMPORT_PROJECTS_FILE_NAME,
IMPORT_LOG_NAME,
FAILED_LOG_NAME,
FAILED_PROJECTS_LOG_NAME,
} from '../../src/common';
import { IMPORT_PROJECTS_FILE_NAME, IMPORT_LOG_NAME } from '../../src/common';
import { deleteTestProjects } from '../delete-test-projects';
import { Project } from '../../src/lib/types';
import { generateLogsPaths } from '../generate-log-file-names';
import { deleteLogs } from '../delete-logs';

const ORG_ID = 'f0125d9b-271a-4b50-ad23-80e12575a1bf';

describe('Import projects script', () => {
const logPath = path.resolve(__dirname, IMPORT_LOG_NAME);
const discoveredProjects: Project[] = [];
let logs: string[];

afterEach(() => {
fs.unlinkSync(logPath);
});
afterAll(async () => {
await deleteTestProjects(ORG_ID, discoveredProjects);
await deleteLogs(logs);
});
it('succeeds to import targets from file', async () => {
const logFiles = generateLogsPaths(__dirname);
logs = Object.values(logFiles);

const projects = await ImportProjects(
path.resolve(__dirname + `/fixtures/${IMPORT_PROJECTS_FILE_NAME}`),
__dirname,
Expand All @@ -35,7 +32,7 @@ describe('Import projects script', () => {
success: true,
targetFile: expect.any(String),
});
const logFile = fs.readFileSync(logPath, 'utf8');
const logFile = fs.readFileSync(logFiles.importLogPath, 'utf8');
expect(logFile).toMatch('shallow-goof-policy');
expect(logFile).toMatch('composer-with-vulns');
expect(logFile).toMatch('ruby-with-versions:');
Expand All @@ -44,60 +41,44 @@ describe('Import projects script', () => {
});

describe('Import skips previously imported', () => {
const logPath = path.resolve(
__dirname + '/fixtures/with-import-log',
IMPORT_LOG_NAME,
);
const OLD_ENV = process.env;
afterEach(async () => {
process.env = { ...OLD_ENV };
}, 1000);
it('succeeds to import targets from file', async () => {
const logPath = path.resolve(__dirname + '/fixtures/with-import-log');
const logFiles = generateLogsPaths(logPath);

const projects = await ImportProjects(
path.resolve(
__dirname + `/fixtures/with-import-log/${IMPORT_PROJECTS_FILE_NAME}`,
),
__dirname + '/fixtures/with-import-log',
)
);
expect(projects.length === 0).toBeTruthy();
const logFile = fs.readFileSync(logPath, 'utf8');
console.log()
const logFile = fs.readFileSync(logFiles.importLogPath, 'utf8');
expect(logFile).toMatchSnapshot();
}, 30000000);
});

describe('Skips & logs issues', () => {
const OLD_ENV = process.env;
const discoveredProjects: Project[] = [];
let logs: string[];

afterEach(() => {
const importsInitiatedLog = path.resolve(
process.env.SNYK_LOG_PATH as string,
IMPORT_LOG_NAME,
);
const importsFailedLog = path.resolve(
process.env.SNYK_LOG_PATH as string,
FAILED_LOG_NAME,
);
const projectsFailedLog = path.resolve(
process.env.SNYK_LOG_PATH as string,
FAILED_PROJECTS_LOG_NAME,
);
[importsFailedLog, importsInitiatedLog, projectsFailedLog].forEach(
(path) => {
try {
fs.unlinkSync(path);
fs.unlinkSync(path);
fs.unlinkSync(path);
} catch (e) {
// do nothing
}
},
);

afterEach(async () => {
await deleteLogs(logs);
process.env = { ...OLD_ENV };
}, 1000);

afterAll(async () => {
await deleteTestProjects(ORG_ID, discoveredProjects);
});
it('Skips any badly formatted targets', async () => {
const logRoot = __dirname + '/fixtures/invalid-target/';
const logs = generateLogsPaths(logRoot)
const logFiles = generateLogsPaths(logRoot);
logs = Object.values(logFiles);

const projects = await ImportProjects(
path.resolve(
__dirname +
Expand All @@ -107,25 +88,19 @@ describe('Skips & logs issues', () => {
expect(projects.length === 0).toBeTruthy();
let logFile = null;
try {
logFile = fs.readFileSync(logs.importLogPath, 'utf8');
logFile = fs.readFileSync(logFiles.importLogPath, 'utf8');
} catch (e) {
expect(logFile).toBeNull();
}
const failedLog = fs.readFileSync(logs.failedImportLogPath, 'utf8');
const failedLog = fs.readFileSync(logFiles.failedImportLogPath, 'utf8');
expect(failedLog).toMatch('shallow-goof-policy');
}, 300);

it('Logs failed when API errors', async () => {
const logRoot = __dirname + '/fixtures/single-project/';
process.env.SNYK_LOG_PATH = logRoot;
const logPath = path.resolve(
process.env.SNYK_LOG_PATH as string,
IMPORT_LOG_NAME,
);
const failedImportsLogPath = path.resolve(
process.env.SNYK_LOG_PATH as string,
FAILED_LOG_NAME,
);
const logFiles = generateLogsPaths(logRoot);
logs = Object.values(logFiles);

process.env.SNYK_HOST = 'https://do-not-exist.com';
const projects = await ImportProjects(
path.resolve(
Expand All @@ -134,45 +109,37 @@ describe('Skips & logs issues', () => {
);
let logFile = null;
try {
logFile = fs.readFileSync(logPath, 'utf8');
logFile = fs.readFileSync(logFiles.importLogPath, 'utf8');
} catch (e) {
expect(logFile).toBeNull();
}
expect(projects.length === 0).toBeTruthy();
const failedLog = fs.readFileSync(failedImportsLogPath, 'utf8');
const failedLog = fs.readFileSync(logFiles.failedImportLogPath, 'utf8');
expect(failedLog).toMatch('ruby-with-versions');
}, 300);
it('Logs failed projects', async () => {
const logRoot = __dirname + '/fixtures/projects-with-errors/';
process.env.SNYK_LOG_PATH = logRoot;
const logPath = path.resolve(
process.env.SNYK_LOG_PATH as string,
IMPORT_LOG_NAME,
);
const failedImportLogPath = path.resolve(
process.env.SNYK_LOG_PATH as string,
FAILED_LOG_NAME,
);
const failedProjectsLogPath = path.resolve(
process.env.SNYK_LOG_PATH as string,
FAILED_PROJECTS_LOG_NAME,
);
const logFiles = generateLogsPaths(logRoot);
logs = Object.values(logFiles);
const projects = await ImportProjects(
path.resolve(
__dirname + '/fixtures/projects-with-errors/import-projects.json',
),
);
const logFile = fs.readFileSync(logPath, 'utf8');
const logFile = fs.readFileSync(logFiles.importLogPath, 'utf8');
expect(logFile).not.toBeNull();
const failedProjectsLog = fs.readFileSync(failedProjectsLogPath, 'utf-8');
const failedProjectsLog = fs.readFileSync(
logFiles.failedProjectsLogPath,
'utf-8',
);
expect(failedProjectsLog).not.toBeNull();
expect(failedProjectsLog).toMatch(
'ruby-app-cyclic-lockfile-master/Gemfile.lock',
);

let failedImportLog = null;
try {
failedImportLog = fs.readFileSync(failedImportLogPath, 'utf8');
failedImportLog = fs.readFileSync(logFiles.importLogPath, 'utf8');
} catch (e) {
expect(failedImportLog).toBeNull();
}
Expand Down

0 comments on commit b019998

Please sign in to comment.