Skip to content

Commit 48b8fdb

Browse files
Add test scripts
1 parent ba1ffef commit 48b8fdb

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

test/01.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import GitInfo from 'react-git-info/macro';
2+
const fs = require('fs');
3+
4+
const gitInfo = GitInfo();
5+
6+
describe('Git information', () => {
7+
test('gets correct branch', () => {
8+
expect(gitInfo.branch).toBe('master');
9+
});
10+
11+
test('gets correct tags', () => {
12+
expect(gitInfo.tags).toHaveLength(2);
13+
expect(new Set(gitInfo.tags)).toEqual(new Set(['hello-world', 'hello-world-again']));
14+
});
15+
});

test/02.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import GitInfo from 'react-git-info/macro';
2+
3+
const gitInfo = GitInfo();
4+
5+
describe('Git information', () => {
6+
test('gets correct branch', () => {
7+
expect(gitInfo.branch).toBe('master');
8+
});
9+
10+
test('gets correct tags', () => {
11+
expect(gitInfo.tags).toHaveLength(0);
12+
});
13+
14+
test('gets correct message', () => {
15+
expect(gitInfo.commit.message).toBe('Git commit message');
16+
})
17+
});

test/03.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import GitInfo from 'react-git-info/macro';
2+
const { execSync } = require('child_process')
3+
4+
const commitHash = execSync('git rev-parse HEAD').toString().trim();
5+
const commitDate = execSync('git log --format=%cI -n 1 HEAD').toString().trim();
6+
const shortCommitHash = execSync('git rev-parse --short HEAD').toString().trim();
7+
8+
const gitInfo = GitInfo();
9+
10+
describe('Git information', () => {
11+
test('gets correct branch', () => {
12+
expect(gitInfo.branch).toBeUndefined();
13+
});
14+
15+
test('gets correct hash', () => {
16+
expect(gitInfo.commit.hash).toBe(commitHash);
17+
});
18+
19+
test('get correct short hash', () => {
20+
expect(gitInfo.commit.shortHash).toBe(shortCommitHash);
21+
})
22+
23+
test('gets correct commit date', () => {
24+
expect(gitInfo.commit.date).toBe(commitDate);
25+
});
26+
});

test/GitInfoTest.ps1

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
$ErrorActionPreference = 'Stop'
2+
Push-Location
3+
4+
# This script requires following to be on the PATH.
5+
$dependencies = 'git', 'npm', 'npx', 'yarn'
6+
foreach ($dependency in $dependencies) {
7+
if ($null -eq (Get-Command $dependency -ErrorAction SilentlyContinue)) {
8+
throw "Unable to find $dependency in PATH"
9+
}
10+
}
11+
12+
$oldEnvCI = $env:CI
13+
$env:CI = 'true' # needed for non-interactive `yarn test`
14+
$scriptsPath = Split-Path $PSCommandPath -Parent
15+
$repoPath = Split-Path $scriptsPath -Parent
16+
17+
# Create a temporary directory for testing
18+
$tempPath = [System.IO.Path]::GetTempPath()
19+
[string] $name = [System.Guid]::NewGuid()
20+
$testRepoPath = Join-Path $tempPath $name
21+
New-Item -ItemType Directory -Path $testRepoPath | Out-Null
22+
23+
# Create a react app using create-react-app
24+
Set-Location $testRepoPath
25+
npx create-react-app .
26+
if (-not $?) {
27+
throw 'Unable to create react app'
28+
}
29+
30+
# Install the package
31+
Write-Output "Installing local package from $repoPath..."
32+
yarn add $repoPath
33+
if (-not $?) {
34+
throw 'Local package installation failed.'
35+
}
36+
37+
38+
Write-Output '------------------- Test 1 -------------------'
39+
40+
$testFile = '01.test.js'
41+
$testScript = Join-Path $testRepoPath "src/$testFile"
42+
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
43+
44+
$commandError = $false
45+
46+
git tag 'hello-world'
47+
$commandError = $commandError -or -not $?
48+
git tag 'hello-world-again'
49+
$commandError = $commandError -or -not $?
50+
51+
if ($commandError) {
52+
throw 'Unable to run git commands.'
53+
}
54+
55+
yarn test
56+
if (-not $?) {
57+
throw "One or more tests failed. Exit code = $LASTEXITCODE"
58+
}
59+
60+
Remove-Item -Force $testScript
61+
62+
63+
Write-Output '------------------- Test 2 -------------------'
64+
65+
$testFile = '02.test.js'
66+
$testScript = Join-Path $testRepoPath "src/$testFile"
67+
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
68+
69+
git commit --allow-empty -m 'Git commit message'
70+
$commandError = $commandError -or -not $?
71+
72+
if ($commandError) {
73+
throw 'Unable to run git commands.'
74+
}
75+
76+
yarn test
77+
if (-not $?) {
78+
throw "One or more tests failed. Exit code = $LASTEXITCODE"
79+
}
80+
81+
Remove-Item -Force $testScript
82+
83+
84+
Write-Output '------------------- Test 3 -------------------'
85+
86+
# tests for detached HEAD
87+
88+
$testFile = '03.test.js'
89+
$testScript = Join-Path $testRepoPath "src/$testFile"
90+
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
91+
92+
$firstCommit = (git rev-list --max-parents=0 HEAD | Out-String).Trim()
93+
$commandError = $commandError -or -not $?
94+
git checkout $firstCommit
95+
$commandError = $commandError -or -not $?
96+
97+
if ($commandError) {
98+
throw 'Unable to run git commands.'
99+
}
100+
101+
yarn test
102+
if (-not $?) {
103+
throw "One or more tests failed. Exit code = $LASTEXITCODE"
104+
}
105+
106+
Remove-Item -Force $testScript
107+
108+
109+
Write-Output '------------------- Cleanup -------------------'
110+
111+
Pop-Location
112+
$env:CI = $oldEnvCI
113+
Remove-Item -Recurse -Force $testRepoPath

0 commit comments

Comments
 (0)