git-utils.test.ts
1 import { 2 copyAssets, 3 setRepo, 4 getUserName, 5 getUserEmail, 6 setCommitAuthor, 7 getCommitMessage 8 } from '../src/git-utils'; 9 import {getInputs} from '../src/get-inputs'; 10 import {Inputs} from '../src/interfaces'; 11 import {getWorkDirName, createDir} from '../src/utils'; 12 import {CmdResult} from '../src/interfaces'; 13 import * as exec from '@actions/exec'; 14 import {cp, rm} from 'shelljs'; 15 import path from 'path'; 16 import fs from 'fs'; 17 18 const testRoot = path.resolve(__dirname); 19 20 async function createTestDir(name: string): Promise<string> { 21 const date = new Date(); 22 const unixTime = date.getTime(); 23 return await getWorkDirName(`${unixTime}_${name}`); 24 } 25 26 beforeEach(() => { 27 jest.resetModules(); 28 process.env['GITHUB_ACTOR'] = 'default-octocat'; 29 process.env['GITHUB_REPOSITORY'] = 'owner/repo'; 30 }); 31 32 afterEach(() => { 33 delete process.env['GITHUB_ACTOR']; 34 delete process.env['GITHUB_REPOSITORY']; 35 }); 36 37 describe('copyAssets', () => { 38 let gitTempDir = ''; 39 (async (): Promise<void> => { 40 const date = new Date(); 41 const unixTime = date.getTime(); 42 gitTempDir = await getWorkDirName(`${unixTime}_git`); 43 })(); 44 45 beforeAll(async () => { 46 await createDir(gitTempDir); 47 process.chdir(gitTempDir); 48 await exec.exec('git', ['init']); 49 }); 50 51 test('copy assets from publish_dir to root, delete .github', async () => { 52 const publishDir = await createTestDir('src'); 53 const destDir = await createTestDir('dst'); 54 cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_1'), publishDir); 55 cp('-Rf', gitTempDir, destDir); 56 57 await copyAssets(publishDir, destDir, '.github'); 58 expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy(); 59 expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); 60 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); 61 rm('-rf', publishDir, destDir); 62 }); 63 64 test('copy assets from publish_dir to root, delete .github,main.js', async () => { 65 const publishDir = await createTestDir('src'); 66 const destDir = await createTestDir('dst'); 67 cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_1'), publishDir); 68 cp('-Rf', gitTempDir, destDir); 69 70 await copyAssets(publishDir, destDir, '.github,main.js'); 71 expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy(); 72 expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); 73 expect(fs.existsSync(path.resolve(destDir, 'main.js'))).toBeFalsy(); 74 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); 75 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.js'))).toBeTruthy(); 76 rm('-rf', publishDir, destDir); 77 }); 78 79 test('copy assets from publish_dir to root, delete nothing', async () => { 80 const publishDir = await createTestDir('src'); 81 const destDir = await createTestDir('dst'); 82 cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir); 83 cp('-Rf', gitTempDir, destDir); 84 85 await copyAssets(publishDir, destDir, ''); 86 expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeTruthy(); 87 expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); 88 expect(fs.existsSync(path.resolve(destDir, 'main.js'))).toBeTruthy(); 89 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); 90 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.js'))).toBeTruthy(); 91 rm('-rf', publishDir, destDir); 92 }); 93 94 test('copy assets from root to root, delete .github', async () => { 95 const publishDir = await createTestDir('src'); 96 const destDir = await createTestDir('dst'); 97 cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir); 98 cp('-Rf', gitTempDir, destDir); 99 cp('-Rf', gitTempDir, publishDir); 100 101 await copyAssets(publishDir, destDir, '.github'); 102 expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy(); 103 expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); 104 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); 105 rm('-rf', publishDir, destDir); 106 }); 107 108 test('copy assets from root to root, delete nothing', async () => { 109 const publishDir = await createTestDir('src'); 110 const destDir = await createTestDir('dst'); 111 cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir); 112 cp('-Rf', gitTempDir, destDir); 113 cp('-Rf', gitTempDir, publishDir); 114 115 await copyAssets(publishDir, destDir, ''); 116 expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeTruthy(); 117 expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); 118 expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); 119 rm('-rf', publishDir, destDir); 120 }); 121 122 test.todo('copy assets from root to subdir, delete .github'); 123 test.todo('copy assets from root to subdir, delete .github,main.js'); 124 test.todo('copy assets from root to subdir, delete nothing'); 125 }); 126 127 describe('setRepo()', () => { 128 test('throw error destination_dir should be a relative path', async () => { 129 process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token'; 130 process.env['INPUT_PUBLISH_BRANCH'] = 'gh-pages'; 131 process.env['INPUT_PUBLISH_DIR'] = 'public'; 132 process.env['INPUT_DESTINATION_DIR'] = '/subdir'; 133 // process.env['INPUT_EXTERNAL_REPOSITORY'] = 'user/repo'; 134 // process.env['INPUT_ALLOW_EMPTY_COMMIT'] = 'true'; 135 // process.env['INPUT_KEEP_FILES'] = 'true'; 136 // process.env['INPUT_FORCE_ORPHAN'] = 'true'; 137 // process.env['INPUT_USER_NAME'] = 'username'; 138 // process.env['INPUT_USER_EMAIL'] = 'github@github.com'; 139 // process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature'; 140 // process.env['INPUT_FULL_COMMIT_MESSAGE'] = 'feat: Add new feature'; 141 // process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3'; 142 // process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3'; 143 // process.env['INPUT_DISABLE_NOJEKYLL'] = 'true'; 144 // process.env['INPUT_CNAME'] = 'github.com'; 145 process.env['INPUT_EXCLUDE_ASSETS'] = '.github'; 146 const inps: Inputs = getInputs(); 147 const remoteURL = 'https://x-access-token:pat@github.com/actions/pages.git'; 148 const date = new Date(); 149 const unixTime = date.getTime(); 150 const workDir = await getWorkDirName(`${unixTime}`); 151 await expect(setRepo(inps, remoteURL, workDir)).rejects.toThrowError( 152 'destination_dir should be a relative path' 153 ); 154 }); 155 }); 156 157 describe('getUserName()', () => { 158 test('get default git user name', () => { 159 const userName = ''; 160 const test = getUserName(userName); 161 expect(test).toMatch('default-octocat'); 162 }); 163 164 test('get custom git user name', () => { 165 const userName = 'custom-octocat'; 166 const test = getUserName(userName); 167 expect(test).toMatch(userName); 168 }); 169 }); 170 171 describe('getUserEmail()', () => { 172 test('get default git user email', () => { 173 const userEmail = ''; 174 const test = getUserEmail(userEmail); 175 expect(test).toMatch('default-octocat@users.noreply.github.com'); 176 }); 177 178 test('get custom git user email', () => { 179 const userEmail = 'custom-octocat@github.com'; 180 const test = getUserEmail(userEmail); 181 expect(test).toMatch(userEmail); 182 }); 183 }); 184 185 describe('setCommitAuthor()', () => { 186 let workDirName = ''; 187 (async (): Promise<void> => { 188 const date = new Date(); 189 const unixTime = date.getTime(); 190 workDirName = await getWorkDirName(`${unixTime}`); 191 })(); 192 193 beforeEach(async () => { 194 await createDir(workDirName); 195 process.chdir(workDirName); 196 await exec.exec('git', ['init']); 197 }); 198 199 test('get default commit author', async () => { 200 const userName = ''; 201 const userEmail = ''; 202 const result: CmdResult = { 203 exitcode: 0, 204 output: '' 205 }; 206 const options = { 207 listeners: { 208 stdout: (data: Buffer): void => { 209 result.output += data.toString(); 210 } 211 } 212 }; 213 await setCommitAuthor(userName, userEmail); 214 result.exitcode = await exec.exec('git', ['config', 'user.name'], options); 215 expect(result.output).toMatch('default-octocat'); 216 result.exitcode = await exec.exec('git', ['config', 'user.email'], options); 217 expect(result.output).toMatch('default-octocat@users.noreply.github.com'); 218 }); 219 220 test('get custom commit author', async () => { 221 const userName = 'custom-octocat'; 222 const userEmail = 'custom-octocat@github.com'; 223 const result: CmdResult = { 224 exitcode: 0, 225 output: '' 226 }; 227 const options = { 228 listeners: { 229 stdout: (data: Buffer): void => { 230 result.output += data.toString(); 231 } 232 } 233 }; 234 await setCommitAuthor(userName, userEmail); 235 result.exitcode = await exec.exec('git', ['config', 'user.name'], options); 236 expect(result.output).toMatch(userName); 237 result.exitcode = await exec.exec('git', ['config', 'user.email'], options); 238 expect(result.output).toMatch(userEmail); 239 }); 240 241 test('throw error user_email is undefined', async () => { 242 const userName = 'custom-octocat'; 243 const userEmail = ''; 244 await expect(setCommitAuthor(userName, userEmail)).rejects.toThrowError( 245 'user_email is undefined' 246 ); 247 }); 248 249 test('throw error user_name is undefined', async () => { 250 const userName = ''; 251 const userEmail = 'custom-octocat@github.com'; 252 await expect(setCommitAuthor(userName, userEmail)).rejects.toThrowError( 253 'user_name is undefined' 254 ); 255 }); 256 }); 257 258 describe('getCommitMessage()', () => { 259 test('get default message', () => { 260 const test = getCommitMessage('', '', '', 'actions/pages', 'commit_hash'); 261 expect(test).toMatch('deploy: commit_hash'); 262 }); 263 264 test('get default message for external repository', () => { 265 const test = getCommitMessage( 266 '', 267 '', 268 'actions/actions.github.io', 269 'actions/pages', 270 'commit_hash' 271 ); 272 expect(test).toMatch('deploy: actions/pages@commit_hash'); 273 }); 274 275 test('get custom message', () => { 276 const test = getCommitMessage('Custom msg', '', '', 'actions/pages', 'commit_hash'); 277 expect(test).toMatch('Custom msg commit_hash'); 278 }); 279 280 test('get custom message for external repository', () => { 281 const test = getCommitMessage( 282 'Custom msg', 283 '', 284 'actions/actions.github.io', 285 'actions/pages', 286 'commit_hash' 287 ); 288 expect(test).toMatch('Custom msg actions/pages@commit_hash'); 289 }); 290 291 test('get full custom message', () => { 292 const test = getCommitMessage('', 'Full custom msg', '', 'actions/pages', 'commit_hash'); 293 expect(test).toMatch('Full custom msg'); 294 }); 295 296 test('get full custom message for external repository', () => { 297 const test = getCommitMessage( 298 '', 299 'Full custom msg', 300 'actions/actions.github.io', 301 'actions/pages', 302 'commit_hash' 303 ); 304 expect(test).toMatch('Full custom msg'); 305 }); 306 });