-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_push_test.py
77 lines (62 loc) · 4.21 KB
/
pre_push_test.py
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
import os
import unittest
from TNTGitHook.pre_push import PrePush
from TNTGitHook.utils import hook_installation_path
class PrePushTestCase(unittest.TestCase):
pre_push: PrePush
def setUp(self) -> None:
self.pre_push = PrePush()
def test_it_can_write_the_pre_push(self):
path_to_write = "./test_pre_push.sh"
self.pre_push.write_in_file(path_to_write, self.pre_push.__str__())
self.assertTrue(self.pre_push.is_already_a_pre_push_file(path_to_write))
os.remove(path_to_write)
def test_the_pre_push_contains_itself(self):
path_to_write = "./test_pre_push.sh"
self.pre_push.write_in_file(path_to_write, self.pre_push.__str__())
self.assertTrue(self.pre_push.is_pre_push_in_file(path_to_write))
os.remove(path_to_write)
def test_the_pre_push_string_is_correct(self):
self.assertEqual(self.pre_push.__str__(),
"#!/bin/bash\n"
"set -o pipefail\n"
"read local_ref local_sha remote_ref remote_sha\n"
f"$HOME/.tnt/hook/bin/tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $(git rev-parse --show-toplevel)")
def test_the_pre_push_string_is_correct_when_there_are_more_elements(self):
self.assertTrue(self.pre_push.is_pre_push_correct(
"#!/bin/bash\n"
"read local_ref local_sha remote_ref remote_sha\n"
"npm run test:ci\n"
f"$HOME/.tnt/hook/bin/tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $(git rev-parse --show-toplevel)"))
def test_the_pre_push_is_composed_correctly(self):
self.assertEqual(self.pre_push.compose_pre_hook("#!/bin/sh\nnpm run test:ci"),
"#!/bin/bash\n"
"set -o pipefail\n"
"read local_ref local_sha remote_ref remote_sha\n"
"npm run test:ci\n"
f"$HOME/.tnt/hook/bin/tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $(git rev-parse --show-toplevel)")
def test_the_pre_push_is_composed_correctly_if_should_be_total_replaced(self):
pre_push_script = f"#!/bin/bash\n" \
f"read local_ref local_sha remote_ref remote_sha\n" \
f"tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $(git rev-parse --show-toplevel)"
self.assertEqual(self.pre_push.compose_pre_hook(pre_push_script),
"#!/bin/bash\n"
"set -o pipefail\n"
"read local_ref local_sha remote_ref remote_sha\n"
f"$HOME/.tnt/hook/bin/tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $(git rev-parse --show-toplevel)")
def test_old_lines_are_removed(self):
self.assertListEqual([],
self.pre_push.remove_old_script_lines(["read local_ref local_sha remote_ref remote_sha",
"# Assumes tnt_git_hook.sh is on PATH",
"PROJECT_PATH=$(git rev-parse --show-toplevel)",
"set -o pipefail",
f"$HOME/.tnt/hook/bin/tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $PROJECT_PATH"]))
self.assertListEqual(["npm run test:ci"],
self.pre_push.remove_old_script_lines(["read local_ref local_sha remote_ref remote_sha",
"# Assumes tnt_git_hook.sh is on PATH",
"npm run test:ci",
"PROJECT_PATH=$(git rev-parse --show-toplevel)",
"set -o pipefail",
f"$HOME/.tnt/hook/bin/tnt_git_hook $local_ref $local_sha $remote_ref $remote_sha $PROJECT_PATH"]))
if __name__ == '__main__':
unittest.main()