1- import { Args , writeEnv } from '../src/main'
1+ import { Args , writeToEnvFile } from '../src/main'
22import * as process from 'process'
33import * as cp from 'child_process'
44import * as path from 'path'
55import * as fs from 'fs'
66
77const ARTIFACTS_PATH = path . join ( __dirname , 'artifacts' )
8+ const ENV_PREFIX = 'ACTION_CREATE_ENV_'
89
910beforeEach ( ( ) => fs . mkdirSync ( ARTIFACTS_PATH ) )
1011afterEach ( ( ) => fs . rmSync ( ARTIFACTS_PATH , { recursive : true , force : true } ) )
12+ afterEach ( ( ) => {
13+ Object . keys ( process . env )
14+ . filter ( key => key . startsWith ( ENV_PREFIX ) )
15+ . forEach ( key => {
16+ delete process . env [ key ]
17+ } )
18+ } )
1119
1220test ( 'throws if <directory> is not found' , async ( ) => {
1321 const args : Args = {
1422 directory : `${ ARTIFACTS_PATH } /this_dir_does_not_exist` ,
15- full_text : ''
23+ full_text : '' ,
24+ include_env_vars : false
1625 }
17- await expect ( writeEnv ( args ) )
26+ await expect ( writeToEnvFile ( args ) )
1827 . rejects
1928 . toThrow ( `Invalid directory input: ${ args . directory } doesn't exist.` )
2029} )
2130
2231test ( 'throws if <directory> is not a directory' , async ( ) => {
2332 const args : Args = {
2433 directory : `${ ARTIFACTS_PATH } /file` ,
25- full_text : ''
34+ full_text : '' ,
35+ include_env_vars : false
2636 }
2737 fs . writeFileSync ( args . directory , '' )
28- await expect ( writeEnv ( args ) )
38+ await expect ( writeToEnvFile ( args ) )
2939 . rejects
3040 . toThrow ( `Invalid directory input: ${ args . directory } is not a directory.` )
3141} )
@@ -36,9 +46,10 @@ test('creates .env', async () => {
3646 full_text : `
3747 PROD=0
3848 TEST=1\n
39- `
49+ ` ,
50+ include_env_vars : false
4051 }
41- await writeEnv ( args )
52+ await writeToEnvFile ( args )
4253 const content = fs . readFileSync ( `${ args . directory } /.env` ) . toString ( )
4354 expect ( content ) . toEqual ( 'PROD=0\nTEST=1' )
4455} )
@@ -52,15 +63,28 @@ test('overwrites .env', async () => {
5263 directory : ARTIFACTS_PATH ,
5364 full_text : `
5465 PROD=0
55- `
66+ ` ,
67+ include_env_vars : false
5668 }
5769 const filePath = `${ args . directory } /.env`
5870 fs . writeFileSync ( filePath , initialText )
59- await writeEnv ( args )
71+ await writeToEnvFile ( args )
6072 const content = fs . readFileSync ( filePath ) . toString ( )
6173 expect ( content ) . toEqual ( 'PROD=0' )
6274} )
6375
76+ test ( 'includes env vars' , async ( ) => {
77+ const args : Args = {
78+ directory : ARTIFACTS_PATH ,
79+ full_text : 'PROD=0\n' , // should be trimmed
80+ include_env_vars : true
81+ }
82+ process . env [ `${ ENV_PREFIX } _${ ENV_PREFIX } _TEST` ] = '1' // prefix should only be removed once
83+ await writeToEnvFile ( args )
84+ const content = fs . readFileSync ( `${ args . directory } /.env` ) . toString ( )
85+ expect ( content ) . toEqual ( `PROD=0\n_${ ENV_PREFIX } _TEST=1` )
86+ } )
87+
6488// shows how the runner will run a javascript action with env / stdout protocol
6589test ( 'test runs' , ( ) => {
6690 process . env [ 'INPUT_FULL_TEXT' ] = 'PROD=0'
0 commit comments