forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignature.js
More file actions
96 lines (80 loc) · 2.66 KB
/
signature.js
File metadata and controls
96 lines (80 loc) · 2.66 KB
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
var assert = require("assert");
var path = require("path");
var local = path.join.bind(path, __dirname);
var promisify = require("promisify-node");
var Promise = require("nodegit-promise");
// Have to wrap exec, since it has a weird callback signature.
var exec = promisify(function(command, opts, callback) {
return require("child_process").exec(command, opts, callback);
});
describe("Signature", function() {
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var Signature = NodeGit.Signature;
var reposPath = local("../repos/workdir");
var name = "Bob Gnarley";
var email = "gnarlee@bob.net";
var arbitraryDate = 123456789;
var timezoneOffset = 60;
it("can be created at an arbitrary time", function() {
var create = Signature.create;
var signature = create(name, email, arbitraryDate, timezoneOffset);
assert.equal(signature.name(), name);
assert.equal(signature.email(), email);
assert.equal(signature.when().time(), arbitraryDate);
assert.equal(signature.when().offset(), 60);
});
it("can be created now", function() {
var signature = Signature.now(name, email);
var now = new Date();
var when = signature.when();
var diff = Math.abs(when.time() - now/1000);
assert.equal(signature.name(), name);
assert.equal(signature.email(), email);
assert(diff <= 1);
// libgit2 does its timezone offsets backwards from javascript
assert.equal(when.offset(), -now.getTimezoneOffset());
});
it("can get a default signature when no user name is set", function() {
var savedUserName;
var savedUserEmail;
var cleanUp = function() {
return exec("git config --global user.name \"" + savedUserName + "\"")
.then(function() {
return exec(
"git config --global user.email \"" +
savedUserEmail +
"\"");
});
};
return exec("git config --global user.name")
.then(function(userName) {
savedUserName = userName.trim();
return exec("git config --global user.email");
})
.then(function(userEmail) {
savedUserEmail = userEmail.trim();
return exec("git config --global --unset user.name");
})
.then(function() {
return exec("git config --global --unset user.email");
})
.then(function() {
return Repository.open(reposPath);
})
.then(function(repo) {
var sig = repo.defaultSignature();
assert.equal(sig.name(), "unknown");
assert.equal(sig.email(), "unknown@unknown.com");
})
.then(function() {
cleanUp();
})
.catch(function(e) {
cleanUp()
.then(function() {
return Promise.reject(e);
});
});
});
});