forked from ocaml/opam
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
executable file
·152 lines (124 loc) · 4.23 KB
/
tests.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python
import unittest
from subprocess import Popen, STDOUT, PIPE
from subprocess import call
import uuid
import os,sys,time
import argparse
import difflib
tmpdir = "/tmp/OPAM.UNITTEST"
results = "results"
opamcmd = "/tmp/OPAM.BIN/opam --yes --root /tmp/OPAM.ROOT"
verbose=0
FNULL=open(os.devnull, "w")
def diff(fromfile,tofile,verbose=0):
fromdate = time.ctime(os.stat(fromfile).st_mtime)
todate = time.ctime(os.stat(tofile).st_mtime)
fromlines = open(fromfile, 'U').readlines()
tolines = open(tofile, 'U').readlines()
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile)#, fromdate, todate)
l = list(diff)
if l :
print "File Differences : "
print ''.join(l)
return False
else :
return True
def opam(cmd, diffile=None,verbose=0):
# first we exectute the action, then
# we compare the output of list with
# the diffile
env = os.environ.copy()
env['PATH'] = ':'.join(['/tmp/OPAM.BIN',env['PATH']])
env['OPAM_ROOT'] = '/tmp/OPAM.ROOT'
cmd = opamcmd.split() + cmd
if verbose > 1 :
print "Env\nPATH=%s\nOPAM_ROOT=%s" % (env['PATH'],env['OPAM_ROOT'])
print "CMD=%s" % (' '.join(cmd))
if verbose <= 1 :
call(cmd, stdout=FNULL, env=env)
else :
call(cmd, env=env)
if diffile :
f = "%s/%s.opamtest" % (tmpdir,uuid.uuid1())
output = open(f,'w')
call(opamcmd.split() + ["list"], stdout=output, env=env)
output.close()
d = diff(f,diffile,verbose)
os.remove(f)
return d
return None
def load_scenario(scenario,verbose=0):
if verbose > 0 :
print "Loading scenario %d" % scenario
if verbose <= 1 :
call(["./init-repo.sh", "-s", str(scenario)],stdout=FNULL)
else :
call(["./init-repo.sh", "-s", str(scenario)])
class OpamTests(unittest.TestCase):
def setUp(self):
if verbose > 0 : print "\nsetting up repository"
call(["./init-repo.sh", "-i"], stdout=FNULL)
if not os.path.exists(tmpdir):
os.makedirs(tmpdir)
def tearDown(self):
if verbose > 0 : print "tearing down repository"
call(["./init-repo.sh", "-c"], stdout=FNULL)
def test_install(self):
load_scenario(1,verbose)
diffile="%s/install-P1" % results
d = opam(["install", "P1"],diffile,verbose)
self.assertTrue(d)
def test_install_many(self):
load_scenario(1,verbose)
diffile="%s/install-P1-P2-P3-P4" % results
opam(["install", "P1"])
opam(["install", "P2"])
opam(["install", "P3"])
d = opam(["install", "P4"], diffile,verbose)
self.assertTrue(d)
def test_remove(self):
load_scenario(1,verbose)
diffile="%s/install-remove-P1" % results
opam(["install", "P1"])
d = opam(["remove", "P1"],diffile,verbose)
self.assertTrue(d)
def test_upgrade(self):
load_scenario(1,verbose)
diffile="%s/install-upgrade-P2" % results
opam(["install", "P4"])
d = opam(["upgrade", "P2"],diffile,verbose)
self.assertTrue(d)
# @unittest.skip("skipping")
def test_reinstall(self):
load_scenario(1,verbose)
diffile="%s/reinstall-P2" % results
opam(["install", "P2"])
d = opam(["reinstall", "P2"],diffile,verbose)
self.assertTrue(d)
def test_install_opt(self):
load_scenario(1,verbose)
load_scenario(2,verbose)
diffile="%s/install-opt" % results
opam(["install", "P5"])
opam(["install", "P2"])
opam(["remove", "P5"])
opam(["remove", "P2"])
d = opam(["remove", "P1"],diffile,verbose)
self.assertTrue(d)
def main():
global verbose
parser = argparse.ArgumentParser(description='description of you program')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-d', '--debug', action='store_true')
args = parser.parse_args()
verbosity=0
if args.verbose == True :
verbose = 1
verbosity=2
if args.debug == True :
verbose = 2
suite = unittest.TestLoader().loadTestsFromTestCase(OpamTests)
unittest.TextTestRunner(verbosity=verbosity).run(suite)
if __name__ == '__main__':
main()