Debian Codegraph @

python/mozversioncontrol/test/test_commit.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 from datetime import datetime
6 from pathlib import Path
7
8 import mozunit
9
10 from mozversioncontrol import get_repository_object
11
12 STEPS = {
13 "hg": [
14 "",
15 """
16 echo "bar" >> bar
17 echo "baz" > foo
18 """,
19 ],
20 "git": [
21 "",
22 """
23 echo "bar" >> bar
24 echo "baz" > foo
25 """,
26 ],
27 "jj": [
28 """
29 jj describe -m 'Ignore file for testing'
30 echo foo > .gitignore
31 jj new
32 """,
33 """
34 echo "bar" >> bar
35 echo "baz" > foo
36 """,
37 ],
38 }
39
40
41 def test_commit(repo):
42 vcs = get_repository_object(repo.dir)
43 assert vcs.working_directory_clean()
44
45 # Setup step for jj to allow untracked changes.
46 repo.execute_next_step()
47
48 # Modify both foo and bar
49 repo.execute_next_step()
50 if repo.vcs != "jj":
51 # jj never has a dirty working directory.
52 assert not vcs.working_directory_clean()
53
54 date_string = "2017-07-14 02:40:00 +0000"
55
56 # Commit just bar
57 vcs.commit(
58 message="Modify bar\n\nbut not baz",
59 author="Testing McTesterson <[email protected]>",
60 date=date_string,
61 paths=["bar"],
62 )
63
64 original_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S %z")
65 date_from_vcs = vcs.get_last_modified_time_for_file(Path("bar"))
66
67 assert original_date == date_from_vcs
68
69 # We only committed bar, so foo is still keeping the working dir dirty. jj
70 # always treats the working directory as clean, because the top commit holds
71 # any changes in it.
72 if repo.vcs == "jj":
73 assert vcs.working_directory_clean()
74 else:
75 assert not vcs.working_directory_clean()
76
77 if repo.vcs == "git":
78 log_cmd = ["log", "-1", "--format=%an,%ae,%aD,%B"]
79 patch_cmd = ["log", "-1", "-p"]
80 elif repo.vcs == "hg":
81 log_cmd = [
82 "log",
83 "-l",
84 "1",
85 "-T",
86 "{person(author)},{email(author)},{date|rfc822date},{desc}",
87 ]
88 patch_cmd = ["log", "-l", "1", "-p"]
89 elif repo.vcs == "jj":
90 log_cmd = [
91 "log",
92 "-n1",
93 "--no-graph",
94 "-r@-",
95 "-T",
96 'separate(",", author.name(), author.email(), commit_timestamp(self).format("%a, %d %b %Y %H:%M:%S %z"), description)',
97 ]
98 patch_cmd = ["show", "@-"]
99
100 # Verify commit metadata (we rstrip to normalize trivial differences)
101 log = vcs._run(*log_cmd).rstrip()
102 assert log == (
103 "Testing McTesterson,[email protected],Fri, 14 "
104 "Jul 2017 02:40:00 +0000,Modify bar\n\nbut not baz"
105 )
106
107 # Verify only the intended file was added to the commit
108 patch = vcs._run(*patch_cmd)
109 diffs = [line for line in patch.splitlines() if "diff --git" in line]
110 assert len(diffs) == 1
111 assert diffs[0] == "diff --git a/bar b/bar"
112
113
114 if __name__ == "__main__":
115 mozunit.main()