From d4b04a6425c4e1169fa3a61c959624e3eaa6f6e5 Mon Sep 17 00:00:00 2001 From: saark-snyk Date: Mon, 20 Dec 2021 09:24:38 +0200 Subject: [PATCH 1/2] chore: reorganize interfaces exports --- src/analysis.ts | 57 ++++++++++---------- src/bundles.ts | 6 ++- src/files.ts | 18 ++----- src/index.ts | 13 +++-- src/interfaces/analysis-options.interface.ts | 20 +++++++ src/interfaces/files.interface.ts | 9 ++++ tests/analysis.spec.ts | 6 +-- 7 files changed, 77 insertions(+), 52 deletions(-) diff --git a/src/analysis.ts b/src/analysis.ts index ce4c4f30..25262d94 100644 --- a/src/analysis.ts +++ b/src/analysis.ts @@ -1,7 +1,7 @@ /* eslint-disable no-await-in-loop */ import omit from 'lodash.omit'; -import { AnalyzeFoldersOptions, prepareExtendingBundle, resolveBundleFilePath, calcHash } from './files'; +import { prepareExtendingBundle, resolveBundleFilePath, calcHash } from './files'; import { POLLING_INTERVAL } from './constants'; import { GetAnalysisErrorCodes, @@ -10,13 +10,19 @@ import { Result, GetAnalysisResponseDto, AnalysisFailedResponse, - AnalysisOptions, - ConnectionOptions, GetAnalysisOptions, } from './http'; -import { createBundleFromFolders, FileBundle, remoteBundleFactory } from './bundles'; +import { createBundleFromFolders, remoteBundleFactory } from './bundles'; import { emitter } from './emitter'; -import { AnalysisResult, AnalysisResultLegacy, AnalysisResultSarif, AnalysisFiles, Suggestion } from './interfaces/analysis-result.interface'; +import { + AnalysisResult, + AnalysisResultLegacy, + AnalysisResultSarif, + AnalysisFiles, + Suggestion, +} from './interfaces/analysis-result.interface'; +import { FileAnalysisOptions } from './interfaces/analysis-options.interface'; +import { FileAnalysis } from './interfaces/files.interface'; const sleep = (duration: number) => new Promise(resolve => setTimeout(resolve, duration)); @@ -86,17 +92,6 @@ function normalizeResultFiles(files: AnalysisFiles, baseDir: string): AnalysisFi return files; } -interface FileAnalysisOptions { - connection: ConnectionOptions; - analysisOptions: AnalysisOptions; - fileOptions: AnalyzeFoldersOptions; -} - -export interface FileAnalysis extends FileAnalysisOptions { - fileBundle: FileBundle; - analysisResults: AnalysisResult; -} - export async function analyzeFolders(options: FileAnalysisOptions): Promise { const fileBundle = await createBundleFromFolders({ ...options.connection, @@ -120,19 +115,24 @@ export async function analyzeFolders(options: FileAnalysisOptions): Promise { +export async function extendAnalysis(options: FileAnalysis & { files: string[] }): Promise { const { files, removedFiles } = await prepareExtendingBundle( options.fileBundle.baseDir, options.fileBundle.supportedFiles, @@ -303,7 +298,13 @@ export async function extendAnalysis(options: ExtendAnalysisOptions): Promise> { +async function* prepareRemoteBundle( + options: PrepareRemoteBundleOptions, +): AsyncGenerator> { let response: Result; let { bundleHash } = options; let cumulativeProgress = 0; diff --git a/src/files.ts b/src/files.ts index 0a8459cf..796adad9 100644 --- a/src/files.ts +++ b/src/files.ts @@ -17,7 +17,7 @@ import { CACHE_KEY, DOTSNYK_FILENAME, } from './constants'; - +import { CollectBundleFilesOptions } from './interfaces/analysis-options.interface'; import { SupportedFiles, FileInfo } from './interfaces/files.interface'; const isWindows = nodePath.sep === '\\'; @@ -225,18 +225,6 @@ async function* searchFiles( } } -export interface AnalyzeFoldersOptions { - paths: string[]; - symlinksEnabled?: boolean; - defaultFileIgnores?: string[]; -} - -export interface CollectBundleFilesOptions extends AnalyzeFoldersOptions { - supportedFiles: SupportedFiles; - baseDir: string; - fileIgnores: string[]; -} - /** * Returns bundle files from requested paths * */ @@ -350,7 +338,7 @@ export async function prepareExtendingBundle( }; } -export function getBundleFilePath(filePath: string, baseDir: string) { +export function getBundleFilePath(filePath: string, baseDir: string): string { const relPath = baseDir ? nodePath.relative(baseDir, filePath) : filePath; // relPath without explicit base makes no sense const posixPath = !isWindows ? relPath : relPath.replace(/\\/g, '/'); return encodeURI(posixPath); @@ -358,7 +346,7 @@ export function getBundleFilePath(filePath: string, baseDir: string) { export function calcHash(content: string): string { return crypto.createHash(HASH_ALGORITHM).update(content).digest(ENCODE_TYPE); -}; +} export async function getFileInfo( filePath: string, diff --git a/src/index.ts b/src/index.ts index 2a6b2415..a856c6de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,20 @@ -import { analyzeFolders, extendAnalysis, FileAnalysis } from './analysis'; +import { analyzeFolders, extendAnalysis } from './analysis'; import { createBundleFromFolders } from './bundles'; import { emitter } from './emitter'; import { startSession, checkSession, getAnalysis, getIpFamily, IpFamily } from './http'; import * as constants from './constants'; import { getGlobPatterns } from './files'; -import { SupportedFiles } from './interfaces/files.interface'; +import { SupportedFiles, FileAnalysis } from './interfaces/files.interface'; import { AnalysisSeverity } from './interfaces/analysis-options.interface'; -import { AnalysisResult, AnalysisResultLegacy, FilePath, FileSuggestion, Suggestion, Marker } from './interfaces/analysis-result.interface'; +import { + AnalysisResult, + AnalysisResultLegacy, + FilePath, + FileSuggestion, + Suggestion, + Marker, +} from './interfaces/analysis-result.interface'; export { getGlobPatterns, diff --git a/src/interfaces/analysis-options.interface.ts b/src/interfaces/analysis-options.interface.ts index 3a94eec5..66265794 100644 --- a/src/interfaces/analysis-options.interface.ts +++ b/src/interfaces/analysis-options.interface.ts @@ -1,6 +1,26 @@ +import { SupportedFiles } from '..'; +import { AnalysisOptions, ConnectionOptions } from '../http'; + // eslint-disable-next-line import/prefer-default-export, no-shadow export enum AnalysisSeverity { info = 1, warning = 2, critical = 3, } +export interface FileAnalysisOptions { + connection: ConnectionOptions; + analysisOptions: AnalysisOptions; + fileOptions: AnalyzeFoldersOptions; +} + +export interface AnalyzeFoldersOptions { + paths: string[]; + symlinksEnabled?: boolean; + defaultFileIgnores?: string[]; +} + +export interface CollectBundleFilesOptions extends AnalyzeFoldersOptions { + supportedFiles: SupportedFiles; + baseDir: string; + fileIgnores: string[]; +} diff --git a/src/interfaces/files.interface.ts b/src/interfaces/files.interface.ts index 100a7b67..87472079 100644 --- a/src/interfaces/files.interface.ts +++ b/src/interfaces/files.interface.ts @@ -1,3 +1,7 @@ +import { AnalysisResult } from '..'; +import { FileBundle } from '../bundles'; +import { FileAnalysisOptions } from './analysis-options.interface'; + export interface File { hash: string; content: string; @@ -19,3 +23,8 @@ export type SupportedFiles = { configFiles: string[]; extensions: string[]; }; + +export interface FileAnalysis extends FileAnalysisOptions { + fileBundle: FileBundle; + analysisResults: AnalysisResult; +} diff --git a/tests/analysis.spec.ts b/tests/analysis.spec.ts index 02b34e9f..4a561819 100644 --- a/tests/analysis.spec.ts +++ b/tests/analysis.spec.ts @@ -1,7 +1,7 @@ import path from 'path'; import jsonschema from 'jsonschema'; -import { analyzeFolders, extendAnalysis, FileAnalysis } from '../src/analysis'; +import { analyzeFolders, extendAnalysis } from '../src/analysis'; import { uploadRemoteBundle } from '../src/bundles'; import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base'; import { sampleProjectPath, bundleFiles, bundleFilesFull, bundleExtender } from './constants/sample'; @@ -262,9 +262,7 @@ describe('Functional test of analysis', () => { if (!sampleRes) return; // TS trick expect(sampleRes.ruleIndex).toBeDefined(); if (!sampleRes.ruleIndex) return; // TS trick - expect(sampleRes!.ruleId).toEqual( - sarifResults.runs[0].tool.driver.rules![sampleRes!.ruleIndex!].id, - ); + expect(sampleRes!.ruleId).toEqual(sarifResults.runs[0].tool.driver.rules![sampleRes!.ruleIndex!].id); expect(extendedBundle.analysisResults.timing.analysis).toBeGreaterThanOrEqual( extendedBundle.analysisResults.timing.fetchingCode, From db9b78428389eb8f7d44e931064e9d216df267e0 Mon Sep 17 00:00:00 2001 From: saark-snyk Date: Mon, 20 Dec 2021 13:37:57 +0200 Subject: [PATCH 2/2] test: fix tests --- tests/analysis.spec.ts | 2 +- tests/api.spec.ts | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/analysis.spec.ts b/tests/analysis.spec.ts index 4a561819..3914b3d4 100644 --- a/tests/analysis.spec.ts +++ b/tests/analysis.spec.ts @@ -247,7 +247,7 @@ describe('Functional test of analysis', () => { const sarifResults = extendedBundle.analysisResults.sarif; - expect(sarifResults.runs[0].tool.driver.rules).toHaveLength(7); + expect(sarifResults.runs[0].tool.driver.rules).toHaveLength(8); expect(sarifResults.runs[0].results).toHaveLength(15); const getRes = (path: string) => sarifResults.runs[0].results!.find( diff --git a/tests/api.spec.ts b/tests/api.spec.ts index a056c4c3..d9e12ff0 100644 --- a/tests/api.spec.ts +++ b/tests/api.spec.ts @@ -2,13 +2,12 @@ import pick from 'lodash.pick'; import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base'; import { bundleFiles, bundleFilesFull } from './constants/sample'; -import { emitter } from '../src/emitter'; import { getFilters, createBundle, checkBundle, extendBundle, getAnalysis, AnalysisStatus } from '../src/http'; import { BundleFiles } from '../src/interfaces/files.interface'; -const fakeBundleHash = '3691c255a7fa00b47bb3ca96593f7108f3539cbe4bc6836e20e5c43410971979'; +const fakeBundleHash = '66f93024eec238dc454fad6bb8617ce17001c27396336358f200052faff13449'; let fakeBundleHashFull = ''; -const realBundleHash = '7f1817d92710efc50f3b68a5f5dcca2bfb7d0ce82db78f2ecd30fd9e9a410059'; +const realBundleHash = ''; let realBundleHashFull = ''; const fakeMissingFiles = [ @@ -35,6 +34,8 @@ describe('Requests to public API', () => { '.cs', '.c', '.cc', + '.cjs', + '.cls', '.cpp', '.cs', '.cxx', @@ -52,12 +53,15 @@ describe('Requests to public API', () => { '.java', '.js', '.jsx', + '.kt', + '.mjs', '.php', '.phtml', '.py', '.rb', '.rhtml', '.slim', + '.swift', '.ts', '.tsx', '.vue', @@ -71,7 +75,6 @@ describe('Requests to public API', () => { }); it('test network issues', async () => { - const response = await getFilters('https://faketest.snyk.io', 'test-source', 1); expect(response.type).toEqual('error'); if (response.type !== 'error') return; @@ -192,7 +195,7 @@ describe('Requests to public API', () => { }); expect(response.type).toEqual('success'); if (response.type === 'error') return; - expect(response.value.bundleHash).toContain('587a6bcb0095606ad57ccc7bb7ac6401475ce4181c13f7136491a16df06544f1'); + expect(response.value.bundleHash).toContain('1484a1a5cf09854080e7be7ed023fd085287d5cf71d046aed47d2c03de1190c6'); expect(response.value.missingFiles).toEqual([`new.js`]); }, TEST_TIMEOUT, @@ -237,7 +240,7 @@ describe('Requests to public API', () => { }); expect(response.type).toEqual('success'); if (response.type !== 'success') return; // TS trick - expect(response.value.bundleHash).toContain('7b0c0099abe1224f0ef92f6a4a0973ec02fa8f57357ec2e7a4e852738bf75178'); + expect(response.value.bundleHash).toContain('77d2fc40d9b77f3e8cfc3b5046e634e5edd958ea3a9b00fb12051fa30942d61f'); expect(response.value.missingFiles).toHaveLength(11); }, TEST_TIMEOUT, @@ -290,7 +293,7 @@ describe('Requests to public API', () => { if (response.type === 'error') return; expect(response.value.status !== AnalysisStatus.failed).toBeTruthy(); - if (response.value.status === AnalysisStatus.complete && response.value.type === 'sarif' ) { + if (response.value.status === AnalysisStatus.complete && response.value.type === 'sarif') { expect(response.value.sarif.runs[0].results).toHaveLength(17); expect(new Set(response.value.coverage)).toEqual(