import { describe , it , afterEach } from 'mocha' ; import assert from 'assert' ; import sinon from 'sinon' ; import nock from 'nock' ; import mockedEnv from 'mocked-env' ; import { RestoreFn } from 'mocked-env' ; import fs from 'fs' ; import * as cli from '../src/cli' ; const sandbox = sinon . createSandbox ( ) ; nock . disableNetConnect ( ) ; describe ( 'cli' , ( ) => { let restore ; afterEach ( ( ) => { sandbox . restore ( ) ; nock . cleanAll ( ) ; if ( restore ) { restore ( ) ; } } ) ; it ( 'should throw if no token is available' , async ( ) => { restore = mockedEnv ( { GITHUB_TOKEN : undefined , GH_TOKEN : undefined , } ) ; await assert . rejects ( async ( ) => cli . parser ( ) . exitProcess ( false ) . parse ( 'dump' ) , /Missing required argument: github-token/ ) ; } ) ; it ( 'should save a file if token is provided' , async ( ) => { const writeFileSyncStub = sandbox . stub ( fs , 'writeFileSync' ) ; restore = mockedEnv ( { GITHUB_TOKEN : 'mygithubtoken' , } ) ; const scope = nock ( 'https://api.github.com' ) . get ( '/orgs/googleapis/repos' ) . reply ( 200 , [ { name : 'repo1' } ] ) . get ( '/orgs/GoogleCloudPlatform/repos' ) . reply ( 200 , [ { name : 'repo2' } ] ) ; await cli . parser ( ) . exitProcess ( false ) . parse ( 'dump -f test.json' ) ; sinon . assert . calledOnceWithExactly ( writeFileSyncStub , 'test.json' , '[{"name":"repo1"},{"name":"repo2"}]' ) ; scope . done ( ) ; } ) ; it ( 'should exit normally with the result' , async ( ) => { let log = '' ; sandbox . replace ( console , 'log' , ( message ? , ... optionalParams ) : void => { log += message ; } ) ; const args = { yamlFiles : [ 'recipes/nodejs.yaml' ] , file : 'test/fixtures/repos.json' , } ; await cli . testYamlCommand ( args ) ; log . includes ( 'Using selector yaml files: recipes/nodejs.yaml' ) ; log . includes ( 'Using dump file: test/fixtures/repos.json' ) ; log . includes ( 'The following repos are hit!' ) ; } ) ; } ) ;