1- import { Downloader , Trivy } from '../src/trivy' ;
2- import { unlinkSync , writeFileSync } from 'fs' ;
1+ import * as fs from 'fs' ;
2+ import { Downloader } from '../src/downloader' ;
3+ import { Trivy } from '../src/trivy' ;
34import { Vulnerability , TrivyOption } from '../src/interface' ;
5+ import { removeTrivyCmd } from './helper' ;
46
57const downloader = new Downloader ( ) ;
68const trivy = new Trivy ( ) ;
79
8- function removeTrivyCmd ( path : string ) {
9- path = path . replace ( / \/ t r i v y $ / , '' ) ;
10- if ( downloader . trivyExists ( path ) ) {
11- unlinkSync ( `${ path } /trivy` ) ;
12- }
13- }
14-
15- describe ( 'Platform' , ( ) => {
16- test ( 'is Liniux' , ( ) => {
17- const result = downloader [ 'checkPlatform' ] ( 'linux' ) ;
18- expect ( result ) . toBe ( 'Linux' ) ;
19- } ) ;
20-
21- test ( 'is Darwin' , ( ) => {
22- const result = downloader [ 'checkPlatform' ] ( 'darwin' ) ;
23- expect ( result ) . toBe ( 'macOS' ) ;
24- } ) ;
25-
26- test ( 'is not linux and darwin' , ( ) => {
27- expect ( ( ) => {
28- downloader [ 'checkPlatform' ] ( 'other' ) ;
29- } ) . toThrowError ( 'Sorry, other is not supported.' ) ;
30- } ) ;
31- } ) ;
32-
33- describe ( 'getDownloadUrl' , ( ) => {
34- test ( 'with latest version and linux' , async ( ) => {
35- const version = 'latest' ;
36- const os = 'Linux' ;
37- const result = await downloader [ 'getDownloadUrl' ] ( version , os ) ;
38- expect ( result ) . toMatch (
39- / r e l e a s e s \/ d o w n l o a d \/ v [ 0 - 9 ] + \. [ 0 - 9 ] + \. [ 0 - 9 ] + \/ t r i v y _ [ 0 - 9 ] + \. [ 0 - 9 ] + \. [ 0 - 9 ] + _ L i n u x - 6 4 b i t \. t a r \. g z $ /
40- ) ;
41- } ) ;
42-
43- test ( 'with 0.2.0 and macOS' , async ( ) => {
44- const version = '0.2.0' ;
45- const os = 'macOS' ;
46- const result = await downloader [ 'getDownloadUrl' ] ( version , os ) ;
47- expect ( result ) . toMatch (
48- / r e l e a s e s \/ d o w n l o a d \/ v 0 \. 2 \. 0 \/ t r i v y _ 0 \. 2 \. 0 _ m a c O S - 6 4 b i t \. t a r \. g z $ /
49- ) ;
50- } ) ;
51-
52- test ( 'with non-supported version' , async ( ) => {
53- const version = 'none' ;
54- const os = 'Linux' ;
55- await expect (
56- downloader [ 'getDownloadUrl' ] ( version , os )
57- ) . rejects . toThrowError (
58- 'Cloud not be found a Trivy asset that you specified.'
59- ) ;
60- } ) ;
61-
62- test ( 'with non-supported os' , async ( ) => {
63- const version = 'latest' ;
64- const os = 'none' ;
65- await expect (
66- downloader [ 'getDownloadUrl' ] ( version , os )
67- ) . rejects . toThrowError (
68- 'Cloud not be found a Trivy asset that you specified.'
69- ) ;
70- } ) ;
71- } ) ;
72-
73- describe ( 'Download trivy command' , ( ) => {
74- afterAll ( ( ) => {
75- removeTrivyCmd ( '__tests__' ) ;
76- } ) ;
77-
78- test ( 'with valid download URL and save in __tests__' , async ( ) => {
79- let downloadUrl = 'https://github.com/aquasecurity/trivy' ;
80- downloadUrl += '/releases/download/v0.2.1/trivy_0.2.1_Linux-64bit.tar.gz' ;
81- const savePath = './__tests__' ;
82- await expect (
83- downloader [ 'downloadTrivyCmd' ] ( downloadUrl , savePath )
84- ) . resolves . toEqual ( `${ savePath } /trivy` ) ;
85- } , 300000 ) ;
86-
87- test ( 'with invalid download URL' , async ( ) => {
88- const downloadUrl = 'https://github.com/this_is_invalid' ;
89- await expect ( downloader [ 'downloadTrivyCmd' ] ( downloadUrl ) ) . rejects . toThrow ( ) ;
90- } ) ;
91- } ) ;
92-
9310describe ( 'Trivy command' , ( ) => {
9411 beforeAll ( ( ) => {
95- writeFileSync ( './trivy' , '' ) ;
12+ fs . writeFileSync ( './trivy' , '' ) ;
9613 } ) ;
9714
9815 afterAll ( ( ) => {
@@ -115,9 +32,9 @@ describe('Trivy scan', () => {
11532 const image : string = 'alpine:3.10' ;
11633
11734 beforeAll ( async ( ) => {
118- trivyPath = ! downloader . trivyExists ( './__tests__' )
119- ? await downloader . download ( 'latest' , './__tests__' )
120- : './__tests__/ trivy' ;
35+ trivyPath = ! downloader . trivyExists ( __dirname )
36+ ? await downloader . download ( 'latest' , __dirname )
37+ : ` ${ __dirname } / trivy` ;
12138 } , 300000 ) ;
12239
12340 afterAll ( ( ) => {
0 commit comments