1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
# Copyright 2013-2017 Vincent Jacques <[email protected]> |
4
|
|
|
|
5
|
|
|
from __future__ import division, absolute_import, print_function |
6
|
|
|
|
7
|
|
|
import ctypes |
8
|
|
|
import subprocess |
9
|
|
|
import sys |
10
|
|
|
import tempfile |
11
|
|
|
import time |
12
|
|
|
import unittest |
13
|
|
|
|
14
|
|
|
try: |
15
|
|
|
import unittest.mock |
16
|
|
|
except ImportError: # Not unittested: test code for Python 2 |
17
|
|
|
import mock |
18
|
|
|
unittest.mock = mock |
19
|
|
|
|
20
|
|
|
from ActionTree import * |
21
|
|
|
|
22
|
|
|
libc = ctypes.CDLL(None) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class TestAction(Action): |
26
|
|
|
def __init__( |
27
|
|
|
self, label, |
28
|
|
|
exception, return_value, delay, |
29
|
|
|
events_file, end_event, |
30
|
|
|
print_on_stdout, print_on_stderr, puts_on_stdout, echo_on_stdout, |
31
|
|
|
accept_failed_dependencies, |
32
|
|
|
): |
33
|
|
|
super(TestAction, self).__init__(label=label, accept_failed_dependencies=accept_failed_dependencies) |
34
|
|
|
self.__exception = exception |
35
|
|
|
self.__return_value = return_value |
36
|
|
|
self.__delay = delay |
37
|
|
|
self.__events_file = events_file |
38
|
|
|
self.__end_event = end_event |
39
|
|
|
self.__print_on_stdout = print_on_stdout |
40
|
|
|
self.__print_on_stderr = print_on_stderr |
41
|
|
|
self.__puts_on_stdout = puts_on_stdout |
42
|
|
|
self.__echo_on_stdout = echo_on_stdout |
43
|
|
|
|
44
|
|
|
def __repr__(self): |
45
|
|
|
return "<TestAction {}>".format(self.label) |
46
|
|
|
|
47
|
|
|
def do_execute(self, dependency_statuses): |
48
|
|
|
for d in self.dependencies: |
49
|
|
|
assert self.accept_failed_dependencies or dependency_statuses[d].status == SUCCESSFUL |
50
|
|
|
with open(self.__events_file, "a") as f: |
51
|
|
|
f.write("{}\n".format(str(self.label).lower())) |
52
|
|
|
if self.__delay: |
53
|
|
|
time.sleep(self.__delay) |
54
|
|
|
if self.__end_event: |
55
|
|
|
with open(self.__events_file, "a") as f: |
56
|
|
|
f.write("{}\n".format(str(self.label).upper())) |
57
|
|
|
if isinstance(self.__print_on_stdout, str): |
58
|
|
|
print(self.__print_on_stdout) |
59
|
|
|
elif isinstance(self.__print_on_stdout, list): |
60
|
|
|
for (p, d) in self.__print_on_stdout: |
61
|
|
|
print(p) |
62
|
|
|
sys.stdout.flush() |
63
|
|
|
time.sleep(d) |
64
|
|
|
if self.__print_on_stderr: |
65
|
|
|
print(self.__print_on_stderr, file=sys.stderr) |
66
|
|
|
if self.__puts_on_stdout: |
67
|
|
|
libc.puts(self.__puts_on_stdout) |
68
|
|
|
if self.__echo_on_stdout: |
69
|
|
|
subprocess.check_call(["echo", self.__echo_on_stdout]) |
70
|
|
|
if self.__exception: |
71
|
|
|
raise self.__exception |
72
|
|
|
else: |
73
|
|
|
return self.__return_value |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class ActionTreeTestCase(unittest.TestCase): |
77
|
|
|
def setUp(self): |
78
|
|
|
# print(self.id()) |
79
|
|
|
(fd, events_file) = tempfile.mkstemp() |
80
|
|
|
os.close(fd) |
81
|
|
|
self.__events_file = events_file |
82
|
|
|
|
83
|
|
|
def tearDown(self): |
84
|
|
|
os.unlink(self.__events_file) |
85
|
|
|
|
86
|
|
|
def _action( |
87
|
|
|
self, label, |
88
|
|
|
exception=None, return_value=None, delay=None, |
89
|
|
|
end_event=False, |
90
|
|
|
print_on_stdout=None, print_on_stderr=None, puts_on_stdout=None, echo_on_stdout=None, |
91
|
|
|
accept_failed_dependencies=False, |
92
|
|
|
*args, **kwds |
93
|
|
|
): |
94
|
|
|
return TestAction( |
95
|
|
|
label, |
96
|
|
|
exception, return_value, delay, |
97
|
|
|
self.__events_file, end_event, |
98
|
|
|
print_on_stdout, print_on_stderr, puts_on_stdout, echo_on_stdout, |
99
|
|
|
accept_failed_dependencies, |
100
|
|
|
*args, **kwds |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
def assertEventsEqual(self, groups): |
104
|
|
|
with open(self.__events_file) as f: |
105
|
|
|
events = [line.strip() for line in f.readlines()] |
106
|
|
|
for group in groups.split(" "): |
107
|
|
|
self.assertEqual(sorted(group), sorted(events[:len(group)])) |
108
|
|
|
events = events[len(group):] |
109
|
|
|
self.assertEqual(events, []) |
110
|
|
|
|
111
|
|
|
def assertEventsIn(self, expected_events): |
112
|
|
|
with open(self.__events_file) as f: |
113
|
|
|
events = [line.strip() for line in f.readlines()] |
114
|
|
|
self.assertIn(events, expected_events) |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
# class TestHooks(Hooks): |
118
|
|
|
# def action_pending(self, time, action): |
119
|
|
|
# print("action_pending", time, action.label) |
120
|
|
|
|
121
|
|
|
# def action_ready(self, time, action): |
122
|
|
|
# print("action_ready", time, action.label) |
123
|
|
|
|
124
|
|
|
# def action_canceled(self, time, action): |
125
|
|
|
# print("action_canceled", time, action.label) |
126
|
|
|
|
127
|
|
|
# def action_started(self, time, action): |
128
|
|
|
# print("action_started", time, action.label) |
129
|
|
|
|
130
|
|
|
# def action_printed(self, time, action, text): |
131
|
|
|
# print("action_printed", time, action.label, text) |
132
|
|
|
|
133
|
|
|
# def action_successful(self, time, action, return_value): |
134
|
|
|
# print("action_successful", time, action.label, return_value) |
135
|
|
|
|
136
|
|
|
# def action_failed(self, time, action, exception): |
137
|
|
|
# print("action_failed", time, action.label, exception) |
138
|
|
|
|