2
2
import { Inputs } from '../src/interfaces' ;
3
3
import { showInputs , getInputs } from '../src/get-inputs' ;
4
4
import os from 'os' ;
5
+ import fs from 'fs' ;
6
+ import yaml from 'js-yaml' ;
5
7
6
8
beforeEach ( ( ) => {
7
9
jest . resetModules ( ) ;
10
+ process . stdout . write = jest . fn ( ) ;
11
+
12
+ const doc = yaml . safeLoad (
13
+ fs . readFileSync ( __dirname + '/../action.yml' , 'utf8' )
14
+ ) ;
15
+ Object . keys ( doc . inputs ) . forEach ( name => {
16
+ const envVar = `INPUT_${ name . replace ( / / g, '_' ) . toUpperCase ( ) } ` ;
17
+ process . env [ envVar ] = doc . inputs [ name ] [ 'default' ] ;
18
+ } ) ;
8
19
} ) ;
9
20
10
21
afterEach ( ( ) => {
11
- delete process . env [ 'INPUT_DEPLOY_KEY' ] ;
12
- delete process . env [ 'INPUT_GITHUB_TOKEN' ] ;
13
- delete process . env [ 'INPUT_PERSONAL_TOKEN' ] ;
14
- delete process . env [ 'INPUT_PUBLISH_BRANCH' ] ;
15
- delete process . env [ 'INPUT_PUBLISH_DIR' ] ;
16
- delete process . env [ 'INPUT_EXTERNAL_REPOSITORY' ] ;
17
- delete process . env [ 'INPUT_ALLOW_EMPTY_COMMIT' ] ;
18
- delete process . env [ 'INPUT_KEEP_FILES' ] ;
19
- delete process . env [ 'INPUT_FORCE_ORPHAN' ] ;
20
- delete process . env [ 'INPUT_USER_NAME' ] ;
21
- delete process . env [ 'INPUT_USER_EMAIL' ] ;
22
- delete process . env [ 'INPUT_COMMIT_MESSAGE' ] ;
23
- delete process . env [ 'INPUT_TAG_NAME' ] ;
24
- delete process . env [ 'INPUT_TAG_MESSAGE' ] ;
25
- delete process . env [ 'INPUT_DISABLE_NOJEKYLL' ] ;
26
- delete process . env [ 'INPUT_CNAME' ] ;
22
+ const doc = yaml . safeLoad (
23
+ fs . readFileSync ( __dirname + '/../action.yml' , 'utf8' )
24
+ ) ;
25
+ Object . keys ( doc . inputs ) . forEach ( name => {
26
+ const envVar = `INPUT_${ name . replace ( / / g, '_' ) . toUpperCase ( ) } ` ;
27
+ console . debug ( `delete ${ envVar } \t${ process . env [ envVar ] } ` ) ;
28
+ delete process . env [ envVar ] ;
29
+ } ) ;
27
30
} ) ;
28
31
29
32
// Assert that process.stdout.write calls called only with the given arguments.
@@ -36,22 +39,6 @@ function assertWriteCalls(calls: string[]): void {
36
39
}
37
40
}
38
41
39
- function setTestInputs ( ) : void {
40
- process . env [ 'INPUT_PUBLISH_BRANCH' ] = 'master' ;
41
- process . env [ 'INPUT_PUBLISH_DIR' ] = 'out' ;
42
- process . env [ 'INPUT_EXTERNAL_REPOSITORY' ] = 'user/repo' ;
43
- process . env [ 'INPUT_ALLOW_EMPTY_COMMIT' ] = 'true' ;
44
- process . env [ 'INPUT_KEEP_FILES' ] = 'true' ;
45
- process . env [ 'INPUT_FORCE_ORPHAN' ] = 'true' ;
46
- process . env [ 'INPUT_USER_NAME' ] = 'username' ;
47
- process . env [ 'INPUT_USER_EMAIL' ] = '[email protected] ' ;
48
- process . env [ 'INPUT_COMMIT_MESSAGE' ] = 'feat: Add new feature' ;
49
- process . env [ 'INPUT_TAG_NAME' ] = 'deploy-v1.2.3' ;
50
- process . env [ 'INPUT_TAG_MESSAGE' ] = 'Deployment v1.2.3' ;
51
- process . env [ 'INPUT_DISABLE_NOJEKYLL' ] = 'true' ;
52
- process . env [ 'INPUT_CNAME' ] = 'github.com' ;
53
- }
54
-
55
42
function getInputsLog ( authMethod : string , inps : Inputs ) : string {
56
43
return `\
57
44
[INFO] ${ authMethod } : true
@@ -66,20 +53,15 @@ function getInputsLog(authMethod: string, inps: Inputs): string {
66
53
[INFO] CommitMessage: ${ inps . CommitMessage }
67
54
[INFO] TagName: ${ inps . TagName }
68
55
[INFO] TagMessage: ${ inps . TagMessage }
69
- [INFO] DisableNoJekyll: ${ inps . DisableNoJekyll }
56
+ [INFO] EnableJekyll ( DisableNoJekyll) : ${ inps . DisableNoJekyll }
70
57
[INFO] CNAME: ${ inps . CNAME }
71
58
` ;
72
59
}
73
60
74
61
describe ( 'showInputs()' , ( ) => {
75
- beforeEach ( ( ) => {
76
- process . stdout . write = jest . fn ( ) ;
77
- } ) ;
78
-
79
62
// eslint-disable-next-line jest/expect-expect
80
63
test ( 'print all inputs DeployKey' , ( ) => {
81
64
process . env [ 'INPUT_DEPLOY_KEY' ] = 'test_deploy_key' ;
82
- setTestInputs ( ) ;
83
65
84
66
const inps : Inputs = getInputs ( ) ;
85
67
showInputs ( inps ) ;
@@ -91,8 +73,8 @@ describe('showInputs()', () => {
91
73
92
74
// eslint-disable-next-line jest/expect-expect
93
75
test ( 'print all inputs GithubToken' , ( ) => {
76
+ delete process . env [ 'INPUT_DEPLOY_KEY' ] ;
94
77
process . env [ 'INPUT_GITHUB_TOKEN' ] = 'test_github_token' ;
95
- setTestInputs ( ) ;
96
78
97
79
const inps : Inputs = getInputs ( ) ;
98
80
showInputs ( inps ) ;
@@ -104,8 +86,9 @@ describe('showInputs()', () => {
104
86
105
87
// eslint-disable-next-line jest/expect-expect
106
88
test ( 'print all inputs PersonalToken' , ( ) => {
89
+ delete process . env [ 'INPUT_DEPLOY_KEY' ] ;
90
+ delete process . env [ 'INPUT_GITHUB_TOKEN' ] ;
107
91
process . env [ 'INPUT_PERSONAL_TOKEN' ] = 'test_personal_token' ;
108
- setTestInputs ( ) ;
109
92
110
93
const inps : Inputs = getInputs ( ) ;
111
94
showInputs ( inps ) ;
@@ -119,10 +102,6 @@ describe('showInputs()', () => {
119
102
describe ( 'getInputs()' , ( ) => {
120
103
test ( 'get default inputs' , ( ) => {
121
104
process . env [ 'INPUT_DEPLOY_KEY' ] = 'test_deploy_key' ;
122
- // process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token';
123
- // process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
124
- process . env [ 'INPUT_PUBLISH_BRANCH' ] = 'gh-pages' ;
125
- process . env [ 'INPUT_PUBLISH_DIR' ] = 'public' ;
126
105
127
106
const inps : Inputs = getInputs ( ) ;
128
107
@@ -181,4 +160,14 @@ describe('getInputs()', () => {
181
160
expect ( inps . DisableNoJekyll ) . toBe ( true ) ;
182
161
expect ( inps . CNAME ) . toMatch ( 'github.com' ) ;
183
162
} ) ;
163
+
164
+ test ( 'throw error enable_jekyll or disable_nojekyll' , ( ) => {
165
+ process . env [ 'INPUT_DEPLOY_KEY' ] = 'test_deploy_key' ;
166
+ process . env [ 'INPUT_ENABLE_JEKYLL' ] = 'true' ;
167
+ process . env [ 'INPUT_DISABLE_NOJEKYLL' ] = 'true' ;
168
+
169
+ expect ( ( ) => {
170
+ getInputs ( ) ;
171
+ } ) . toThrowError ( 'Use either of enable_jekyll or disable_nojekyll' ) ;
172
+ } ) ;
184
173
} ) ;
0 commit comments