Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec7385c

Browse files
author
Kyle Smith
committed
Merged feature/branches-exist into develop
2 parents 95c6de7 + 817363d commit ec7385c

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

spec/tests/Base-spec.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@ const Config = require('../../src/Config');
99
describe('Base', function() {
1010
beforeEach(function() {
1111
const repoConfig = {
12+
getString(key) {
13+
const defaultConfig = Config.getConfigDefault();
14+
return defaultConfig[key] || true;
15+
},
1216
setString() {
1317
return Promise.resolve();
1418
}
1519
};
1620
this.repo = {
1721
config() {
1822
return Promise.resolve(repoConfig);
23+
},
24+
createBranch() {
25+
return Promise.resolve();
26+
},
27+
getBranchCommit() {
28+
return Promise.resolve({id(){ return '12345';}});
1929
}
2030
};
21-
22-
spyOn(NodeGit.Branch, 'lookup').and.returnValue(Promise.resolve());
2331
});
2432

2533
it('should be able to require Base', function() {
@@ -34,6 +42,7 @@ describe('Base', function() {
3442

3543
describe('init', function() {
3644
it('should throw error if no repository is passed', function(done) {
45+
spyOn(NodeGit.Branch, 'lookup').and.returnValue(Promise.resolve());
3746
return Base.init()
3847
.then(jasmine.fail)
3948
.catch((reason) => {
@@ -43,6 +52,7 @@ describe('Base', function() {
4352
});
4453

4554
it('should return new flow object if repository is passed', function(done) {
55+
spyOn(NodeGit.Branch, 'lookup').and.returnValue(Promise.resolve());
4656
const defaultConfig = Config.getConfigDefault();
4757
return Base.init(this.repo, defaultConfig)
4858
.then((flow) => {
@@ -56,12 +66,57 @@ describe('Base', function() {
5666
done();
5767
});
5868
});
69+
70+
it('develop branch should exist after initialization', function(done) {
71+
spyOn(NodeGit.Branch, 'lookup').and.returnValue(Promise.resolve());
72+
const defaultConfig = Config.getConfigDefault();
73+
return Base.init(this.repo, defaultConfig)
74+
.then(() => Base.developBranchExists(this.repo))
75+
.then((exists) => {
76+
expect(exists).toBeTruthy();
77+
done();
78+
});
79+
});
80+
81+
it('develop branch should not exist after initialization and delete develop branch', function(done) {
82+
const defaultConfig = Config.getConfigDefault();
83+
spyOn(NodeGit.Branch, 'lookup').and.callFake((repo, branchName) => {
84+
if (branchName === defaultConfig['gitflow.branch.develop']) {
85+
return Promise.reject(new Error('Could not find branch'));
86+
}
87+
return Promise.resolve();
88+
});
89+
return Base.init(this.repo, defaultConfig)
90+
.then(() => Base.developBranchExists(this.repo))
91+
.then((exists) => {
92+
expect(exists).toBeFalsy();
93+
done();
94+
});
95+
});
96+
97+
it('master branch should not exist after initialization and delete master branch', function(done) {
98+
const defaultConfig = Config.getConfigDefault();
99+
spyOn(NodeGit.Branch, 'lookup').and.callFake((repo, branchName) => {
100+
if (branchName === defaultConfig['gitflow.branch.master']) {
101+
return Promise.reject(new Error('Could not find branch'));
102+
}
103+
return Promise.resolve();
104+
});
105+
return Base.masterBranchExists(this.repo)
106+
.then((exists) => {
107+
expect(exists).toBeFalsy();
108+
done();
109+
});
110+
});
59111
});
60112

61113
describe('open', function() {
114+
beforeEach(function() {
115+
spyOn(NodeGit.Branch, 'lookup').and.returnValue(Promise.resolve());
116+
});
117+
62118
it('should throw error if no repository is passed', function(done) {
63119
return Base.open()
64-
.then(jasmine.fail)
65120
.catch((reason) => {
66121
expect(reason).toEqual(jasmine.any(Error));
67122
done();
@@ -72,7 +127,6 @@ describe('Base', function() {
72127
spyOn(Base, 'isInitialized').and.returnValue(Promise.resolve(false));
73128

74129
return Base.open(this.repo)
75-
.then(jasmine.fail)
76130
.catch((reason) => {
77131
expect(reason).toEqual(jasmine.any(Error));
78132
done();

src/Base.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ function createFlowInstance(repo) {
3131
* @class
3232
*/
3333
class Base {
34+
/**
35+
* Check if the repo is initialized with git flow and its develop branch exists
36+
* @async
37+
* @param {Repository} repo The nodegit repository instance to check
38+
* @return {Boolean} Whether or not the develop branch as specified in the git config exists
39+
*/
40+
static developBranchExists(repo) {
41+
if (!repo) {
42+
return Promise.reject(new Error(constants.ErrorMessage.REPO_REQUIRED));
43+
}
44+
45+
return this.isInitialized(repo)
46+
.then((isInitialized) => {
47+
if (!isInitialized) {
48+
return Promise.reject(new Error(constants.ErrorMessage.GIT_FLOW_NOT_INITIALIZED));
49+
}
50+
51+
return repo.config()
52+
.then((config) => {
53+
const developBranchName = config.getString('gitflow.branch.develop');
54+
return NodeGit.Branch.lookup(repo, developBranchName, NodeGit.Branch.BRANCH.LOCAL);
55+
})
56+
.then(() => true)
57+
.catch(() => false);
58+
});
59+
}
60+
3461
/**
3562
* Initializes the repo to use git flow
3663
* @async
@@ -108,6 +135,33 @@ class Base {
108135
});
109136
}
110137

138+
/**
139+
* Check if the repo is initialized with git flow and its master branch exists
140+
* @async
141+
* @param {Repository} repo The nodegit repository instance to check
142+
* @return {Boolean} Whether or not the master branch as specified in the git config exists
143+
*/
144+
static masterBranchExists(repo) {
145+
if (!repo) {
146+
return Promise.reject(new Error(constants.ErrorMessage.REPO_REQUIRED));
147+
}
148+
149+
return Base.isInitialized(repo)
150+
.then((isInitialized) => {
151+
if (!isInitialized) {
152+
return Promise.reject(new Error(constants.ErrorMessage.GIT_FLOW_NOT_INITIALIZED));
153+
}
154+
155+
return repo.config()
156+
.then((config) => {
157+
const masterBranchName = config.getString('gitflow.branch.master');
158+
return NodeGit.Branch.lookup(repo, masterBranchName, NodeGit.Branch.BRANCH.LOCAL);
159+
})
160+
.then(() => true)
161+
.catch(() => false);
162+
});
163+
}
164+
111165
/**
112166
* Creates a Flow instance for a repo that already has git flow initialized
113167
* @async

src/constants/ErrorMessageConstants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const ErrorMessageConstants = {
2+
GIT_FLOW_NOT_INITIALIZED: 'Git flow has not been initialized on this repo',
23
REPO_REQUIRED: 'A repository is required'
34
};
45

0 commit comments

Comments
 (0)