-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtestGitUtils.js
180 lines (149 loc) · 6.91 KB
/
testGitUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
const assert = require('assert');
const path = require('path');
const net = require('net');
const fse = require('fs-extra');
const shell = require('shelljs');
const git = require('isomorphic-git');
const { createTestRoot } = require('./utils.js');
git.plugins.set('fs', require('fs'));
const GitUtils = require('../src/git-utils');
const GIT_USER_HOME = path.resolve(__dirname, 'fixtures/gitutils');
if (!shell.which('git')) {
shell.echo('Sorry, this tests requires git');
shell.exit(1);
}
describe('Testing GitUtils', () => {
let testRoot;
let pwd;
beforeEach(async () => {
testRoot = await createTestRoot();
await fse.writeFile(path.resolve(testRoot, 'README.md'), 'Hello\n', 'utf-8');
await fse.writeFile(path.resolve(testRoot, '.gitignore'), '.env\n', 'utf-8');
await fse.writeFile(path.resolve(testRoot, '.env'), 'HLX_BLA=123\n', 'utf-8');
// throw a Javascript error when any shell.js command encounters an error
shell.config.fatal = true;
// init git repo
pwd = shell.pwd();
shell.cd(testRoot);
shell.exec('git init');
shell.exec('git add -A');
shell.exec('git commit -m"initial commit."');
});
afterEach(async () => {
shell.cd(pwd);
await fse.remove(testRoot);
});
it('getOrigin #unit', async () => {
shell.exec('git remote add origin http://github.com/adobe/dummy.git');
assert.ok(await GitUtils.getOrigin(testRoot));
assert.ok(/dummy/.test(await GitUtils.getOrigin(testRoot)));
});
it('getOriginURL #unit', async () => {
shell.exec('git remote add origin http://github.com/adobe/dummy.git');
assert.ok(await GitUtils.getOriginURL(testRoot));
assert.equal((await GitUtils.getOriginURL(testRoot)).toString(), 'http://github.com/adobe/dummy.git');
});
it('getBranch #unit', async () => {
shell.exec('git checkout -b newbranch');
assert.equal(await GitUtils.getBranch(testRoot), 'newbranch');
shell.exec('git tag v0.0.0');
assert.equal(await GitUtils.getBranch(testRoot), 'v0.0.0');
});
it('isDirty #unit', async () => {
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await fse.writeFile(path.resolve(testRoot, 'README.md'), 'Hello, world.\n', 'utf-8');
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), true);
});
it('isDirty #unit with submodules', async () => {
// https://github.com/adobe/helix-cli/issues/614
const moduleRoot = await createTestRoot();
await fse.writeFile(path.resolve(moduleRoot, 'README.md'), 'Hello\n', 'utf-8');
const currentPwd = shell.pwd();
shell.cd(moduleRoot);
shell.exec('git init');
shell.exec('git add -A');
shell.exec('git commit -m"initial commit."');
shell.cd(currentPwd);
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
shell.exec(`git submodule add file://${moduleRoot}`);
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), true);
shell.exec('git add -A');
shell.exec('git commit -m "added submodule"');
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await fse.remove(moduleRoot);
});
it('isDirty #unit with new file', async () => {
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await fse.writeFile(path.resolve(testRoot, 'index.md'), 'Hello, world.\n', 'utf-8');
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), true);
});
it('isDirty #unit with staged file', async () => {
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await fse.writeFile(path.resolve(testRoot, 'index.md'), 'Hello, world.\n', 'utf-8');
shell.exec('git add -A');
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), true);
});
it('isDirty #unit with deleted file', async () => {
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await fse.remove(path.resolve(testRoot, 'README.md'));
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), true);
});
it('isDirty #unit with globally excluded file', async () => {
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await fse.writeFile(path.resolve(testRoot, '.global-ignored-file.txt'), 'Hello, world.\n', 'utf-8');
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
});
it('isDirty #unit with unix socket', async function socketTest() {
if (process.platform === 'win32') {
this.skip();
return;
}
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
await new Promise((resolve) => {
const unixSocketServer = net.createServer();
unixSocketServer.listen(path.resolve(testRoot, 'test.sock'), () => {
unixSocketServer.close(resolve);
});
});
assert.equal(await GitUtils.isDirty(testRoot, GIT_USER_HOME), false);
});
it('getBranchFlag #unit', async () => {
assert.equal(await GitUtils.getBranchFlag(testRoot), 'master');
await fse.writeFile(path.resolve(testRoot, 'README.md'), 'Hello, world.\n', 'utf-8');
assert.equal(await GitUtils.getBranchFlag(testRoot), 'dirty');
});
it('getRepository #unit', async () => {
shell.exec('git remote add origin http://github.com/adobe/dummy.git');
assert.equal(await GitUtils.getRepository(testRoot), 'http---github.aaakk.us.kg-adobe-dummy-git');
});
it('getRepository (local) #unit', async () => {
assert.equal(await GitUtils.getRepository(testRoot), `local--${path.basename(testRoot)}`);
});
it('getCurrentRevision #unit', async () => {
assert.ok(/[0-9a-fA-F]+/.test(await GitUtils.getCurrentRevision(testRoot)));
});
it('isIgnored', async () => {
await fse.writeFile(path.resolve(testRoot, '.global-ignored-file.txt'), 'Hello, world.\n', 'utf-8');
assert.ok(await GitUtils.isIgnored(testRoot, '.env', GIT_USER_HOME));
assert.ok(await GitUtils.isIgnored(testRoot, '.global-ignored-file.txt', GIT_USER_HOME));
assert.ok(!(await GitUtils.isIgnored(testRoot, 'README.md', GIT_USER_HOME)));
assert.ok(await GitUtils.isIgnored(testRoot, 'not-existing.md', GIT_USER_HOME));
});
it('isIgnored works outside git', async () => {
const anotherTestRoot = await createTestRoot();
await fse.writeFile(path.resolve(anotherTestRoot, '.env'), 'Hello, world.\n', 'utf-8');
assert.ok(await GitUtils.isIgnored(anotherTestRoot, '.env', GIT_USER_HOME));
});
});