UNPKG

4.94 kBJavaScriptView Raw
1var should = require('should');
2var fetchGitData = require('../lib/fetchGitData');
3var getOptions = require('../index').getOptions;
4
5describe("fetchGitData", function(){
6 beforeEach(function(){
7 process.env = {PATH: process.env.PATH};
8 });
9 it("should throw an error when no data is passed", function() {
10 fetchGitData.should.throw(/fetchGitData requires a callback/);
11 });
12 it('should throw an error when no git context is provided', function(done) {
13 fetchGitData(undefined, function(err){
14 err.should.match(/No options passed/);
15 done();
16 });
17 });
18 it("should throw an error if no head is provided", function(done) {
19 fetchGitData({
20 }, function(err){
21 err.should.match(/You must provide the head/);
22 done();
23 });
24 });
25 it("should throw an error if no head.id is provided", function(done) {
26 fetchGitData({
27 head: {}
28 }, function(err){
29 err.should.match(/You must provide the head.id/);
30 done();
31 });
32 });
33 it("should return default values", function(done) {
34 var options = fetchGitData({
35 head: {
36 id: "COMMIT_HASH"
37 }
38 }, function(err, options){
39 options.should.eql({
40 "head": {
41 "id": "COMMIT_HASH",
42 "author_name": "Unknown Author",
43 "author_email": "",
44 "committer_name": "Unknown Committer",
45 "committer_email": "",
46 "message": "Unknown Commit Message"
47 },
48 "branch": "",
49 "remotes": []
50 });
51 done();
52 });
53 });
54 it("should override default values", function(done) {
55 var options = fetchGitData({
56 "head": {
57 "id": "COMMIT_HASH",
58 "author_name": "MY AUTHOR",
59 "author_email": "",
60 "committer_name": "MY COMMITTER",
61 "committer_email": "",
62 "message": "MY COMMIT MESSAGE"
63 },
64 "branch": "TEST",
65 "remotes": [
66 {
67 "name": "TEST",
68 "url": "test-url"
69 }
70 ]
71 }, function(err, options){
72 options.should.eql({
73 "head": {
74 "id": "COMMIT_HASH",
75 "author_name": "MY AUTHOR",
76 "author_email": "",
77 "committer_name": "MY COMMITTER",
78 "committer_email": "",
79 "message": "MY COMMIT MESSAGE"
80 },
81 "branch": "TEST",
82 "remotes": [
83 {
84 "name": "TEST",
85 "url": "test-url"
86 }
87 ]
88 });
89 done();
90 });
91 });
92 it("should convert git.branch to a string", function(done) {
93 fetchGitData({
94 "head": {
95 "id": "COMMIT_HASH"
96 },
97 "branch": {
98 "covert": "to a string"
99 }
100 }, function(err, str){
101 str.branch.should.be.String();
102 fetchGitData({
103 "head": {
104 "id": "COMMIT_HASH"
105 },
106 "branch": ["convert", "to", "a", "string"]
107 }, function(err, str){
108 str.branch.should.be.String();
109 done();
110 });
111 });
112 });
113 it("should convert git.remotes to an array", function(done) {
114 fetchGitData({
115 "head": {
116 "id": "COMMIT_HASH"
117 },
118 "remotes": "convert from string to an array"
119 }, function(err, arr){
120 arr.remotes.should.be.instanceof(Array);
121 fetchGitData({
122 "head": {
123 "id": "COMMIT_HASH"
124 },
125 "remotes": {
126 "convert": "from object to an array"
127 }
128 }, function(err, arr){
129 arr.remotes.should.be.instanceof(Array);
130 done();
131 });
132 });
133 });
134 it("should save passed remotes", function(done) {
135 fetchGitData({
136 "head": {
137 "id": "COMMIT_HASH"
138 },
139 "remotes": [
140 {
141 "name": "test",
142 "url": "https://my.test.url"
143 }
144 ]
145 }, function(err, options){
146 options.should.eql({
147 "head": {
148 "id": "COMMIT_HASH",
149 "author_name": "Unknown Author",
150 "author_email": "",
151 "committer_name": "Unknown Committer",
152 "committer_email": "",
153 "message": "Unknown Commit Message"
154 },
155 "branch": "",
156 "remotes": [
157 {
158 "name": "test",
159 "url": "https://my.test.url"
160 }
161 ]
162 });
163 done();
164 });
165 });
166 it("should execute git commands when a valid commit hash is given", function(done) {
167 process.env.COVERALLS_GIT_COMMIT = "HEAD";
168 process.env.COVERALLS_GIT_BRANCH = "master";
169 getOptions(function(err, options){
170 options = options.git;
171 options.head.should.be.Object();
172 options.head.author_name.should.not.equal("Unknown Author");
173 options.head.committer_name.should.not.equal("Unknown Committer");
174 options.head.message.should.not.equal("Unknown Commit Message");
175 options.branch.should.be.String();
176 options.should.have.property("remotes");
177 options.remotes.should.be.instanceof(Array);
178 options.remotes.length.should.be.above(0);
179 done();
180 });
181 });
182});