import fs from "fs" ; import os from "os" ; import path from "path" ; import { HLD_COMPONENT_FILENAME , RENDER_HLD_PIPELINE_FILENAME , } from "../../lib/constants" ; import { checkoutCommitPushCreatePRLink } from "../../lib/gitpr" ; import { createTempDir } from "../../lib/ioUtil" ; import { disableVerboseLogging , enableVerboseLogging , logger , } from "../../logger" ; import { execute , initialize } from "./init" ; jest . mock ( "../../lib/gitpr" ) ; jest . mock ( "../../lib/gitutils" ) ; beforeAll ( ( ) => { enableVerboseLogging ( ) ; } ) ; afterAll ( ( ) => { disableVerboseLogging ( ) ; } ) ; const defaultComponentGit = "https://github.com/microsoft/fabrikate-definitions.git" ; const defaultComponentName = "traefik2" ; const defaultComponentPath = "definitions/traefik2" ; const testExecuteFn = async ( projectRootPath , gitPush , componentGit , componentName , componentPath , expectedExitCode ) : Promise < void > => { const exitFn = jest . fn ( ) ; await execute ( projectRootPath , gitPush , componentGit , componentName , componentPath , exitFn ) ; expect ( exitFn ) . toBeCalledTimes ( 1 ) ; expect ( exitFn . mock . calls ) . toEqual ( [ [ expectedExitCode ] ] ) ; } ; describe ( "Test execute function" , ( ) => { it ( "simulate an error: missing project path" , async ( ) => { await testExecuteFn ( "" , false , defaultComponentGit , defaultComponentName , defaultComponentPath , 1 ) ; } ) ; it ( "positive test" , async ( ) => { await testExecuteFn ( os . tmpdir ( ) , false , defaultComponentGit , defaultComponentName , defaultComponentPath , 0 ) ; } ) ; } ) ; const testRepoInitialization = async ( gitPush , componentGit , componentName , componentPath ) : Promise < void > => { const randomTmpDir = createTempDir ( ) ; logger . info ( `creating randomTmpDir ${ randomTmpDir }` ) ; await initialize ( randomTmpDir , gitPush , componentGit , componentName , componentPath ) ; expect ( fs . existsSync ( randomTmpDir ) ) . toBe ( true ) ; [ RENDER_HLD_PIPELINE_FILENAME , HLD_COMPONENT_FILENAME ] . map ( ( filename ) => path . join ( randomTmpDir , filename ) ) . forEach ( ( filePath ) => { expect ( fs . existsSync ( filePath ) ) . toBe ( true ) ; } ) ; } ; describe ( "Initializing an HLD Repository." , ( ) => { test ( "New directory is created under root directory with required service files." , async ( ) => { await testRepoInitialization ( false , defaultComponentGit , defaultComponentName , defaultComponentPath ) ; } ) ; test ( "New directory is created and git push is enabled." , async ( ) => { await testRepoInitialization ( true , defaultComponentGit , defaultComponentName , defaultComponentPath ) ; expect ( checkoutCommitPushCreatePRLink ) . toHaveBeenCalled ( ) ; } ) ; } ) ;