This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGitOperation.test.js
152 lines (148 loc) · 7.71 KB
/
GitOperation.test.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
'use strict'
const expect = require('expect')
const sinon = require('sinon')
const sandbox = sinon.createSandbox()
const GitClient = require('../lib/GitOperation')
const { childProcess } = require('../lib/util-functions')
const path = require('path')
describe('GitOperation:', function() {
this.timeout(50000)
const tempDir = path.join(__dirname, 'tmp/nr-pipeline-ext-PZTj7Z')
beforeEach(async function() {
sandbox.restore()
const fs = require('fs')
await new Promise((resolve, reject) => {
fs.rmdir(tempDir, { recursive: true }, err => {
if (err) throw err
resolve(true)
})
})
})
afterEach(async function() {
sandbox.restore()
const fs = require('fs')
await new Promise((resolve, reject) => {
fs.rmdir(tempDir, { recursive: true }, err => {
if (err) throw err
resolve(true)
})
})
})
// system test, requires credential from "./idir.local.json" (create one if not exists)
context('prepare() @slow', function() {
it.skip('When successfully prepared, repo directory is returned.', function() {
// @ts-ignore
// eslint-disable-next-line require-path-exists/exists
const idir = require('./idir.local.json')
// const gitCredentials = {username: 'fakeUsername', password: 'fakePass', email: 'fakeEmail'}
const gitCredentials = { username: idir.user, password: idir.pass, email: 'fakeEmail' }
const git = new GitClient(
'/Users/iliu/Workspace/labs/tempDir',
'https://bwa.nrs.gov.bc.ca/int/stash/scm/spi/spi-schema-da-review.git',
gitCredentials,
'test'
)
return git.prepare().then(result => {
expect(result).not.toBeNull()
})
})
})
context('isTargetBranchOutofSync()', function() {
it.skip('is not up to date', function() {
const fs = require('fs')
return expect(
new Promise(resolve => {
fs.rmdir(tempDir, { recursive: true }, err => {
if (err) throw err
resolve(tempDir)
})
})
.then(async basedir => {
const pwd = path.join(basedir, 'a')
// console.log(`\ngit init ${pwd}`)
await childProcess('git', ['init', pwd])
await childProcess('git', ['config', '--local', 'user.email', '[email protected]'], { cwd: pwd })
await childProcess('git', ['config', '--local', 'user.name', 'Your Name'], { cwd: pwd })
fs.writeFileSync(path.join(pwd, 'hello'), 'Hello', { encoding: 'utf8' })
await childProcess('git', ['add', 'hello'], { cwd: pwd })
await childProcess('git', ['commit', '-m', '1st Commit'], { cwd: pwd })
await childProcess('git', ['checkout', '-b', 'feature/a'], { cwd: pwd })
fs.writeFileSync(path.join(pwd, 'hello'), ' world!', { encoding: 'utf8', flag: 'a' })
await childProcess('git', ['add', 'hello'], { cwd: pwd })
await childProcess('git', ['commit', '-m', '2nd Commit'], { cwd: pwd })
await childProcess('git', ['checkout', 'master'], { cwd: pwd })
fs.writeFileSync(path.join(pwd, 'hello'), ' people!', { encoding: 'utf8', flag: 'a' })
await childProcess('git', ['add', 'hello'], { cwd: pwd })
await childProcess('git', ['commit', '-m', '3rd Commit'], { cwd: pwd })
await childProcess('git', ['checkout', 'feature/a'], { cwd: pwd })
return basedir
})
.then(async basedir => {
const pwd = path.join(basedir, 'b')
await childProcess('git', ['clone', path.join(basedir, 'a'), pwd])
await childProcess('git', ['config', '--local', 'user.email', '[email protected]'], { cwd: pwd })
await childProcess('git', ['config', '--local', 'user.name', 'Your Name'], { cwd: pwd })
return pwd
})
.then(pwd => {
const settings = { dir: pwd, branch: { name: 'feature/a' }, change: { target: 'master' } }
const git = new GitClient(settings)
const stub = sandbox.stub(git, '_setupCredentials')
stub.callsFake(() => {
return Promise.resolve()
})
return git.isTargetBranchOutofSync().finally(() => {
git.clear()
})
})
).rejects.toThrow()
})
it.skip('is up to date', function() {
const fs = require('fs')
return expect(
new Promise(resolve => {
fs.rmdir(tempDir, { recursive: true }, err => {
if (err) throw err
resolve(tempDir)
})
})
.then(async basedir => {
const pwd = path.join(basedir, 'a')
// console.log(`\ngit init ${pwd}`)
await childProcess('git', ['init', pwd])
await childProcess('git', ['config', '--local', 'user.email', '[email protected]'], { cwd: pwd })
await childProcess('git', ['config', '--local', 'user.name', 'Your Name'], { cwd: pwd })
fs.writeFileSync(path.join(pwd, 'hello'), 'Hello', { encoding: 'utf8' })
await childProcess('git', ['add', 'hello'], { cwd: pwd })
await childProcess('git', ['commit', '-m', '1st Commit'], { cwd: pwd })
await childProcess('git', ['checkout', '-b', 'feature/a'], { cwd: pwd })
fs.writeFileSync(path.join(pwd, 'hello'), ' world!', { encoding: 'utf8', flag: 'a' })
await childProcess('git', ['add', 'hello'], { cwd: pwd })
await childProcess('git', ['commit', '-m', '2nd Commit'], { cwd: pwd })
await childProcess('git', ['checkout', 'master'], { cwd: pwd })
return basedir
})
.then(async basedir => {
const pwd = path.join(basedir, 'b')
await childProcess('git', ['clone', path.join(basedir, 'a'), pwd])
await childProcess('git', ['config', '--local', 'user.email', '[email protected]'], { cwd: pwd })
await childProcess('git', ['config', '--local', 'user.name', 'Your Name'], { cwd: pwd })
return pwd
})
.then(pwd => {
const settings = {
dir: pwd,
url: 'file://localhost/abc.git',
credentials: { username: 'test', password: '123' },
branch: { name: 'feature/a' },
change: { target: 'master' },
}
const git = new GitClient(settings)
return git.isTargetBranchOutofSync().finally(val => {
git.clear()
})
})
).resolves.toBe(true)
})
})
})