import { assert } from 'chai' ; import { ComponentDetails , IArtifact , IGeneral , ILicense } from 'jfrog-client-js' ; import { before } from 'mocha' ; import * as path from 'path' ; import * as Collections from 'typescript-collections' ; import * as vscode from 'vscode' ; import { ConnectionManager } from '../../main/connect/connectionManager' ; import { GoDependencyUpdate } from '../../main/dependencyUpdate/goDependencyUpdate' ; import { FocusType } from '../../main/focus/abstractFocus' ; import { LogManager } from '../../main/log/logManager' ; import { ScanCacheManager } from '../../main/scanCache/scanCacheManager' ; import { ScanLogicManager } from '../../main/scanLogic/scanLogicManager' ; import { DependenciesTreeNode } from '../../main/treeDataProviders/dependenciesTree/dependenciesTreeNode' ; import { TreesManager } from '../../main/treeDataProviders/treesManager' ; import { GeneralInfo } from '../../main/types/generalInfo' ; import { ILicenseCacheObject } from '../../main/types/licenseCacheObject' ; import { ILicenseKey } from '../../main/types/licenseKey' ; import { GoUtils } from '../../main/utils/goUtils' ; import { PackageDescriptorType , ScanUtils } from '../../main/utils/scanUtils' ; import { createScanCacheManager , getNodeByArtifactId } from './utils/utils.test' ; /**
 * Test functionality of @class GoUtils.
 */ describe ( 'Go Utils Tests' , ( ) => { let logManager = new LogManager ( ) . activate ( ) ; let dummyScanCacheManager = createScanCacheManager ( ) ; let treesManager = new TreesManager ( [ ] , new ConnectionManager ( logManager ) , dummyScanCacheManager , { } as ScanLogicManager , logManager ) ; let projectDirs = [ 'dependency' , 'empty' ] ; let goDependencyUpdate = new GoDependencyUpdate ( ) ; let workspaceFolders ; let tmpDir = vscode . Uri . file ( path . join ( __dirname , '..' , 'resources' , 'go' ) ) ; before ( ( ) => { workspaceFolders = [ { uri : tmpDir , name : '' , index : 0 } as vscode . WorkspaceFolder ] ; } ) ; /**
     * Test GoUtils.locateGoMods.
     */ it ( 'Locate go mods' , async ( ) => { let packageDescriptors = await ScanUtils . locatePackageDescriptors ( workspaceFolders , treesManager . logManager ) ; let goMods = packageDescriptors . get ( PackageDescriptorType . GO ) ; assert . isDefined ( goMods ) ; assert . strictEqual ( goMods ?. length , projectDirs . length ) ; for ( let expectedProjectDir of projectDirs ) { let expectedGoMod = path . join ( tmpDir . fsPath , expectedProjectDir , 'go.mod' ) ; assert . isDefined ( goMods ?. find ( goMod => goMod . fsPath === expectedGoMod ) , 'Should contain ' + expectedGoMod ) ; } } ) ; /**
     * Test GoUtils.getDependenciesPos.
     */ it ( 'Get dependencies position' , async ( ) => { let goMod = vscode . Uri . file ( path . join ( tmpDir . fsPath , 'dependency' , 'go.mod' ) ) ; let textDocument = await vscode . workspace . openTextDocument ( goMod ) ; let dependenciesPos = GoUtils . getDependenciesPos ( textDocument ) ; assert . deepEqual ( dependenciesPos [ 0 ] , new vscode . Position ( 4 , 0 ) ) ; assert . deepEqual ( dependenciesPos [ 1 ] , new vscode . Position ( 4 , 7 ) ) ; goMod = vscode . Uri . file ( path . join ( tmpDir . fsPath , 'empty' , 'go.mod' ) ) ; textDocument = await vscode . workspace . openTextDocument ( goMod ) ; dependenciesPos = GoUtils . getDependenciesPos ( textDocument ) ; assert . isEmpty ( dependenciesPos ) ; } ) ; /**
     * Test GoUtils.getDependencyPos.
     */ it ( 'Get dependency position' , async ( ) => { let goMod = vscode . Uri . file ( path . join ( tmpDir . fsPath , 'dependency' , 'go.mod' ) ) ; let textDocument = await vscode . workspace . openTextDocument ( goMod ) ; let dependenciesTreeNode = new DependenciesTreeNode ( new GeneralInfo ( 'github.com/jfrog/jfrog-cli-core' , '1.9.1' , [ ] , '' , '' ) ) ; let dependencyPos = GoUtils . getDependencyPos ( textDocument , dependenciesTreeNode , FocusType . Dependency ) ; assert . deepEqual ( dependencyPos [ 0 ] , new vscode . Position ( 5 , 1 ) ) ; assert . deepEqual ( dependencyPos [ 1 ] , new vscode . Position ( 5 , 39 ) ) ; goMod = vscode . Uri . file ( path . join ( tmpDir . fsPath , 'empty' , 'go.mod' ) ) ; textDocument = await vscode . workspace . openTextDocument ( goMod ) ; dependencyPos = GoUtils . getDependencyPos ( textDocument , dependenciesTreeNode , FocusType . Dependency ) ; assert . isEmpty ( dependencyPos ) ; } ) ; it ( 'Update fixed version' , async ( ) => { let parent = new DependenciesTreeNode ( new GeneralInfo ( 'parent' , '1.0.0' , [ ] , '' , '' ) ) ; let componentsToScan = new Collections . Set ( ) ; await runCreateGoDependenciesTrees ( componentsToScan , parent ) ; let node = getNodeByArtifactId ( parent , 'github.com/jfrog/jfrog-cli-core' ) ; assert . isNotNull ( node ) ; assert . equal ( node ?. generalInfo . version , '1.9.0' ) ; goDependencyUpdate . updateDependencyVersion ( node ! , '1.9.1' ) ; parent = new DependenciesTreeNode ( new GeneralInfo ( 'parent' , '1.0.0' , [ ] , '' , '' ) ) ; await runCreateGoDependenciesTrees ( componentsToScan , parent ) ; node = getNodeByArtifactId ( parent , 'github.com/jfrog/jfrog-cli-core' ) ; assert . isNotNull ( node ) ; assert . equal ( node ?. generalInfo . version , '1.9.1' ) ; goDependencyUpdate . updateDependencyVersion ( node ! , '1.9.0' ) ; parent = new DependenciesTreeNode ( new GeneralInfo ( 'parent' , '1.0.0' , [ ] , '' , '' ) ) ; await runCreateGoDependenciesTrees ( componentsToScan , parent ) ; node = getNodeByArtifactId ( parent , 'github.com/jfrog/jfrog-cli-core' ) ; assert . isNotNull ( node ) ; assert . equal ( node ?. generalInfo . version , '1.9.0' ) ; } ) ; /**
     * Test GoUtils.createGoDependenciesTrees.
     */ it ( 'Create go Dependencies Trees' , async ( ) => { let parent = new DependenciesTreeNode ( new GeneralInfo ( 'parent' , '1.0.0' , [ ] , '' , '' ) ) ; let componentsToScan = new Collections . Set ( ) ; await runCreateGoDependenciesTrees ( componentsToScan , parent ) ; assert . isAbove ( componentsToScan . size ( ) , 0 ) ; let gofrog ; for ( let component of componentsToScan . toArray ( ) ) { if ( component . component_id === 'go://github.com/jfrog/gofrog:1.0.6' ) { gofrog = component ; } } assert . isDefined ( gofrog ) ; assert . deepEqual ( parent . children [ 0 ] . label , 'github.com/shield/black-widow' ) ; assert . deepEqual ( parent . children [ 1 ] . label , 'github.com/shield/falcon' ) ; assert . deepEqual ( parent . children [ 0 ] . parent , parent ) ; assert . deepEqual ( parent . children [ 1 ] . parent , parent ) ; assert . lengthOf ( parent . children [ 0 ] . children , 2 ) ; let child = parent . children [ 0 ] . children [ 0 ] ; assert . deepEqual ( child . componentId , 'github.com/jfrog/jfrog-cli-core:1.9.0' ) ; assert . deepEqual ( child . label , 'github.com/jfrog/jfrog-cli-core' ) ; assert . deepEqual ( child . description , '1.9.0' ) ; assert . deepEqual ( child . parent , parent . children [ 0 ] ) ; let actualLicenseName = child . licenses . toArray ( ) [ 0 ] ; let actualLicense = dummyScanCacheManager . getLicense ( actualLicenseName . licenseName ) ! ; assert . isDefined ( actualLicense ) ; let expectedLicense = xrayScanResults [ 0 ] . licenses ; assert . deepEqual ( actualLicense . name , expectedLicense [ 0 ] . name ) ; assert . deepEqual ( actualLicense . moreInfoUrl , expectedLicense [ 0 ] . more_info_url [ 0 ] ) ; assert . deepEqual ( actualLicense . fullName , expectedLicense [ 0 ] . full_name ) ; } ) ; async function runCreateGoDependenciesTrees ( componentsToScan , parent ) { let packageDescriptors = await ScanUtils . locatePackageDescriptors ( workspaceFolders , treesManager . logManager ) ; let goMods = packageDescriptors . get ( PackageDescriptorType . GO ) ; await GoUtils . createDependenciesTrees ( goMods , componentsToScan , treesManager , parent , false ) ; await dummyScanCacheManager . storeArtifacts ( xrayScanResults ) ; parent . children . forEach ( child => { treesManager . dependenciesTreeDataProvider . addXrayInfoToTree ( child ) ; } ) ; return parent . children . sort ( ( lhs , rhs ) => ( < string > lhs . label ) . localeCompare ( < string > rhs . label ) ) ; } } ) ; const xrayScanResults = [ { general : { component_id : 'github.com/jfrog/jfrog-cli-core:1.9.0' } as IGeneral , issues : [ ] , licenses : [ { name : 'Apache-2.0' , full_name : 'The Apache Software License, Version 2.0' , more_info_url : [ 'http://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt' , 'https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt' , 'http://licenses.nuget.org/Apache-2.0' , 'https://licenses.nuget.org/Apache-2.0' , 'http://www.apache.org/licenses/LICENSE-2.0' , 'https://spdx.org/licenses/Apache-2.0.html' , 'https://spdx.org/licenses/Apache-2.0' , 'http://www.opensource.org/licenses/apache2.0.php' , 'http://www.opensource.org/licenses/Apache-2.0' ] , components : [ 'go://github.com/jfrog/jfrog-cli-core:1.9.1' ] } as ILicense ] } ] ;