2019-11-19 23:06:27 +08:00
// Initial env variable setup for tests.
2020-03-05 21:19:45 +08:00
process . env [ 'INPUT_FOLDER' ] = 'build'
process . env [ 'GITHUB_SHA' ] = '123'
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
import { action } from '../src/constants'
import { deploy , generateBranch , init , switchToBaseBranch } from '../src/git'
import { execute } from '../src/execute'
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
const originalAction = JSON . stringify ( action )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
jest . mock ( '@actions/core' , ( ) = > ( {
2020-01-20 04:33:14 +08:00
setFailed : jest.fn ( ) ,
2020-03-28 22:35:26 +08:00
getInput : jest.fn ( ) ,
isDebug : jest.fn ( ) ,
info : jest.fn ( )
2020-03-05 21:19:45 +08:00
} ) )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
jest . mock ( '../src/execute' , ( ) = > ( {
2019-11-19 23:06:27 +08:00
execute : jest.fn ( )
2020-03-05 21:19:45 +08:00
} ) )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'git' , ( ) = > {
2019-11-19 23:06:27 +08:00
afterEach ( ( ) = > {
2020-03-05 21:19:45 +08:00
Object . assign ( action , JSON . parse ( originalAction ) )
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'init' , ( ) = > {
it ( 'should execute commands if a GitHub token is provided' , async ( ) = > {
2019-12-22 05:51:39 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
repositoryPath : 'JamesIves/github-pages-deploy-action' ,
folder : 'build' ,
branch : 'branch' ,
gitHubToken : '123' ,
2019-12-22 05:51:39 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2019-12-22 05:51:39 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
await init ( action )
expect ( execute ) . toBeCalledTimes ( 6 )
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should execute commands if an Access Token is provided' , async ( ) = > {
2019-11-19 23:06:27 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
repositoryPath : 'JamesIves/github-pages-deploy-action' ,
folder : 'build' ,
branch : 'branch' ,
accessToken : '123' ,
2019-11-19 23:06:27 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2019-12-22 05:51:39 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
await init ( action )
expect ( execute ) . toBeCalledTimes ( 6 )
} )
2019-12-22 05:51:39 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should execute commands if SSH is true' , async ( ) = > {
2020-01-20 05:23:47 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
repositoryPath : 'JamesIves/github-pages-deploy-action' ,
folder : 'build' ,
branch : 'branch' ,
2020-01-20 05:23:47 +08:00
ssh : true ,
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 05:23:47 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-01-20 05:23:47 +08:00
2020-03-05 21:19:45 +08:00
await init ( action )
2020-01-20 05:23:47 +08:00
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 6 )
} )
2020-01-20 05:23:47 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if there is no provided GitHub Token, Access Token or SSH bool' , async ( ) = > {
2019-11-19 23:06:27 +08:00
Object . assign ( action , {
2020-03-02 20:52:38 +08:00
repositoryPath : null ,
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
2019-11-19 23:06:27 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 04:33:14 +08:00
} ,
gitHubToken : null ,
accessToken : null ,
2020-03-29 01:24:51 +08:00
ssh : null
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await init ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 0 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
2020-03-05 21:19:45 +08:00
'There was an error initializing the repository: No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true. ❌'
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if there is no folder' , async ( ) = > {
2020-03-02 20:52:38 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
repositoryPath : 'JamesIves/github-pages-deploy-action' ,
gitHubToken : '123' ,
branch : 'branch' ,
2020-03-02 20:52:38 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-03-02 20:52:38 +08:00
} ,
folder : null ,
ssh : true
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await init ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 0 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
2020-03-05 21:19:45 +08:00
'There was an error initializing the repository: You must provide the action with a folder to deploy. ❌'
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if there is no provided repository path' , async ( ) = > {
2020-03-02 20:52:38 +08:00
Object . assign ( action , {
repositoryPath : null ,
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
2020-03-02 20:52:38 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-03-02 20:52:38 +08:00
} ,
2020-03-05 21:19:45 +08:00
gitHubToken : '123' ,
2020-03-02 20:52:38 +08:00
accessToken : null ,
2020-03-29 01:24:51 +08:00
ssh : null
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await init ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 0 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
2020-03-05 21:19:45 +08:00
'There was an error initializing the repository: No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true. '
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if the build folder begins with a /' , async ( ) = > {
2019-11-19 23:06:27 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
repositoryPath : 'JamesIves/github-pages-deploy-action' ,
branch : 'branch' ,
folder : '/' ,
2019-11-19 23:06:27 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2019-12-22 05:51:39 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await init ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 0 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
"There was an error initializing the repository: Incorrectly formatted build folder. The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly. ❌"
2020-03-05 21:19:45 +08:00
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if the build folder begins with a ./' , async ( ) = > {
2019-11-19 23:06:27 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
branch : 'branch' ,
folder : './' ,
2019-11-19 23:06:27 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2019-12-22 05:51:39 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await init ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 0 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
"There was an error initializing the repository: Incorrectly formatted build folder. The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly. ❌"
2020-03-05 21:19:45 +08:00
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should not fail if root is used' , async ( ) = > {
2019-11-19 23:06:27 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
repositoryPath : 'JamesIves/github-pages-deploy-action' ,
accessToken : '123' ,
branch : 'branch' ,
folder : '.' ,
root : '.' ,
2019-11-19 23:06:27 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2019-12-22 05:51:39 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
await init ( action )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 6 )
} )
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'generateBranch' , ( ) = > {
it ( 'should execute six commands' , async ( ) = > {
2020-01-20 04:33:14 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
branch : 'branch' ,
folder : '.' ,
2020-01-20 04:33:14 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 04:33:14 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
await generateBranch ( action )
expect ( execute ) . toBeCalledTimes ( 6 )
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if there is no branch' , async ( ) = > {
2020-01-20 04:33:14 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
2020-01-20 04:33:14 +08:00
branch : null ,
2020-03-05 21:19:45 +08:00
folder : '.' ,
2020-01-20 04:33:14 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 04:33:14 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-01-20 04:33:14 +08:00
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await generateBranch ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
expect ( e . message ) . toMatch (
2020-03-05 21:19:45 +08:00
'There was an error creating the deployment branch: Branch is required. ❌'
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
} )
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'switchToBaseBranch' , ( ) = > {
it ( 'should execute one command' , async ( ) = > {
2020-01-20 04:33:14 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
branch : 'branch' ,
folder : '.' ,
2020-01-20 04:33:14 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 04:33:14 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
await switchToBaseBranch ( action )
expect ( execute ) . toBeCalledTimes ( 1 )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should execute one command if using custom baseBranch' , async ( ) = > {
2020-03-02 20:52:38 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
baseBranch : '123' ,
accessToken : '123' ,
branch : 'branch' ,
folder : '.' ,
2020-03-02 20:52:38 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
await switchToBaseBranch ( action )
expect ( execute ) . toBeCalledTimes ( 1 )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should fail if one of the required parameters is not available' , async ( ) = > {
2020-03-02 20:52:38 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
baseBranch : '123' ,
2020-03-02 20:52:38 +08:00
accessToken : null ,
gitHubToken : null ,
2020-03-29 01:24:51 +08:00
ssh : null ,
2020-03-05 21:19:45 +08:00
branch : 'branch' ,
2020-03-02 20:52:38 +08:00
folder : null ,
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await switchToBaseBranch ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 0 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
2020-03-05 21:19:45 +08:00
'There was an error switching to the base branch: No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true. ❌'
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
} )
2019-12-22 05:51:39 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'deploy' , ( ) = > {
it ( 'should execute commands' , async ( ) = > {
2019-11-19 23:06:27 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
gitHubToken : '123' ,
2019-11-19 23:06:27 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2019-12-22 05:51:39 +08:00
}
2020-03-05 21:19:45 +08:00
} )
2019-12-22 05:51:39 +08:00
2020-03-05 21:19:45 +08:00
await deploy ( action )
2019-11-19 23:06:27 +08:00
// Includes the call to generateBranch
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 12 )
} )
2020-01-20 04:33:14 +08:00
2020-03-31 08:44:09 +08:00
it ( 'should execute commands with single commit toggled' , async ( ) = > {
Object . assign ( action , {
folder : 'build' ,
branch : 'branch' ,
gitHubToken : '123' ,
singleCommit : true ,
pusher : {
name : 'asd' ,
email : 'as@cat'
}
} )
await deploy ( action )
// Includes the call to generateBranch
expect ( execute ) . toBeCalledTimes ( 18 )
} )
2020-03-05 21:19:45 +08:00
it ( 'should execute commands with clean options, ommits sha commit message' , async ( ) = > {
process . env . GITHUB_SHA = ''
2020-01-20 04:33:14 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
gitHubToken : '123' ,
2020-01-20 04:33:14 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 04:33:14 +08:00
} ,
clean : true ,
cleanExclude : '["cat", "montezuma"]'
2020-03-05 21:19:45 +08:00
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
await deploy ( action )
2020-03-02 20:52:38 +08:00
// Includes the call to generateBranch
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 12 )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should execute commands with clean options stored as an array instead' , async ( ) = > {
2020-03-02 20:52:38 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
gitHubToken : '123' ,
2020-03-02 20:52:38 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-03-02 20:52:38 +08:00
} ,
clean : true ,
2020-03-05 21:19:45 +08:00
cleanExclude : [ 'cat' , 'montezuma' ]
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
await deploy ( action )
2020-01-20 04:33:14 +08:00
// Includes the call to generateBranch
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 12 )
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should gracefully handle incorrectly formatted clean exclude items' , async ( ) = > {
2020-01-20 04:33:14 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
folder : '.' ,
branch : 'branch' ,
gitHubToken : '123' ,
2020-01-20 05:16:28 +08:00
pusher : { } ,
2020-01-20 04:33:14 +08:00
clean : true ,
2020-03-05 21:19:45 +08:00
targetFolder : 'new_folder' ,
commitMessage : 'Hello!' ,
2020-01-20 05:16:28 +08:00
isTest : true ,
2020-01-20 04:33:14 +08:00
cleanExclude : '["cat, "montezuma"]' // There is a syntax errror in the string.
2020-03-05 21:19:45 +08:00
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
await deploy ( action )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 12 )
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should stop early if there is nothing to commit' , async ( ) = > {
2020-01-20 04:33:14 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
gitHubToken : '123' ,
2020-01-20 04:33:14 +08:00
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-01-20 04:33:14 +08:00
} ,
isTest : false // Setting this env variable to false means there will never be anything to commit and the action will exit early.
2020-03-05 21:19:45 +08:00
} )
2020-01-20 04:33:14 +08:00
2020-03-05 21:19:45 +08:00
await deploy ( action )
expect ( execute ) . toBeCalledTimes ( 13 )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should throw an error if one of the required parameters is not available' , async ( ) = > {
2020-03-02 20:52:38 +08:00
Object . assign ( action , {
2020-03-05 21:19:45 +08:00
folder : 'build' ,
branch : 'branch' ,
2020-03-29 01:24:51 +08:00
ssh : null ,
2020-03-02 20:52:38 +08:00
accessToken : null ,
gitHubToken : null ,
pusher : {
2020-03-05 21:19:45 +08:00
name : 'asd' ,
email : 'as@cat'
2020-03-02 20:52:38 +08:00
} ,
isTest : false // Setting this env variable to false means there will never be anything to commit and the action will exit early.
2020-03-05 21:19:45 +08:00
} )
2020-03-02 20:52:38 +08:00
try {
2020-03-05 21:19:45 +08:00
await deploy ( action )
2020-03-02 20:52:38 +08:00
} catch ( e ) {
2020-03-05 21:19:45 +08:00
expect ( execute ) . toBeCalledTimes ( 1 )
2020-03-02 20:52:38 +08:00
expect ( e . message ) . toMatch (
2020-03-05 21:19:45 +08:00
'The deploy step encountered an error: No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true. ❌'
)
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
} )
} )
} )