Merge lp:~widelands-dev/widelands/python3 into lp:widelands

Proposed by Tino
Status: Merged
Merged at revision: 7951
Proposed branch: lp:~widelands-dev/widelands/python3
Merge into: lp:widelands
Diff against target: 135 lines (+38/-27)
1 file modified
regression_test.py (+38/-27)
To merge this branch: bzr merge lp:~widelands-dev/widelands/python3
Reviewer Review Type Date Requested Status
Tino Approve
Klaus Halfmann review / test / compile Approve
GunChleoc Approve
Review via email: mp+291236@code.launchpad.net

Commit message

Add a compatibility layer to regression_test.py to allow running with python 2 and 3

Description of the change

Add a compatibility layer to regression_test.py to allow running with python 2 and 3

To post a comment you must log in.
Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Looks like you know more about python than I do,
lets check this ...

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

OK, thas as far as I came, too but now I get
$ ./regression_test.py -b ./widelands
...
File "./regression_test.py", line 103, in run_widelands
  stdout_file.write(line)
TypeError: must be str, not bytes

I used
   print(line, flush=True)
for
   stdout_file.write(line)
   stdout_file.flush()

but this in not python2.7 compatible :-(

In addition I used

< "Widelands exited abnormally. %s" % common_msg
---
> "Widelands exited abnormally. " + common_msg

< "Not all tests pass. %s." % common_msg
---
> "Not all tests pass. " + common_msg

but perhaos that .format() aproach is more compatible

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Mhh, even using python 2.7 does not work, it just gets stuck in the splasg screeen?
Was there some change in the Lua Binding?

Revision history for this message
Tino (tino79) wrote :

Hm, I did run the test with both Python 2/3 without these errors, but perhaps I aborted them too early.
Or the test do not currently run on windows?

I'll check this later today...

review: Needs Fixing
Revision history for this message
GunChleoc (gunchleoc) wrote :

On my system. this will start the Widelands main screen for each test instead of going to the scenario. Python 2.7

Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 980. State: errored. Details: https://travis-ci.org/widelands/widelands/builds/121409569.
Appveyor build 813. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_python3-813.

Revision history for this message
Tino (tino79) wrote :

Ok, please have another look on linux.

review: Needs Resubmitting
Revision history for this message
GunChleoc (gunchleoc) wrote :

It's working for me now :)

review: Approve
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 985. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/121622180.
Appveyor build 818. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_python3-818.

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

$ ./regression_test.py -b ./widelands ; ## thats Python 3.4.4
Ran 32 tests in 1018.389s

$ python2.7 ./regression_test.py -b ./widelands
...
Ran 32 tests in 616.502s

Works but Python 3.4.4 was much slower.
Well I used this first so all files where cached.

So we had tests on windows, ubuntuntu and OSX, ready for merge?

review: Approve (review / test / compile)
Revision history for this message
Tino (tino79) wrote :

I cannot imagine why Python3 should be slower, because it is a very simple script and all test computing is done by the widelands binary. I am pretty sure if you would do another run with Python 3 it should be equally fast (perhaps your OS or your harddisk caches, or something other was occupying your CPU).

@bunnybot merge

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'regression_test.py'
2--- regression_test.py 2016-02-04 12:21:58 +0000
3+++ regression_test.py 2016-04-08 06:11:28 +0000
4@@ -12,6 +12,22 @@
5 import unittest
6 import platform
7
8+#Python2/3 compat code for iterating items
9+try:
10+ dict.iteritems
11+except AttributeError:
12+ # Python 3
13+ def itervalues(d):
14+ return iter(d.values())
15+ def iteritems(d):
16+ return iter(d.items())
17+else:
18+ # Python 2
19+ def itervalues(d):
20+ return d.itervalues()
21+ def iteritems(d):
22+ return d.iteritems()
23+
24 def datadir():
25 return os.path.join(os.path.dirname(__file__), "data")
26
27@@ -63,20 +79,22 @@
28 impact the filenames for stdout.txt.
29
30 Returns the stdout filename."""
31- stdout_filename = os.path.join(self.run_dir, "stdout_%02i.txt" % which_time)
32+ stdout_filename = os.path.join(self.run_dir, "stdout_{:02d}.txt".format(which_time))
33+ log_filename = os.path.join(self.run_dir, "log_{:02d}.txt".format(which_time))
34 if (os.path.exists(stdout_filename)):
35 os.unlink(stdout_filename)
36
37 with open(stdout_filename, 'a') as stdout_file:
38 args = [self.path_to_widelands_binary,
39 '--verbose=true',
40- '--datadir=%s' % datadir(),
41- '--datadir_for_testing=%s' % datadir_for_testing(),
42- '--homedir=%s' % self.run_dir,
43+ '--datadir={}'.format(datadir()),
44+ '--datadir_for_testing={}'.format(datadir_for_testing()),
45+ '--homedir={}'.format(self.run_dir),
46 '--disable_fx=true',
47 '--disable_music=true',
48- '--language=en_US' ]
49- args += [ "--%s=%s" % (key, value) for key, value in wlargs.iteritems() ]
50+ '--language=en_US',
51+ '--logfile={}'.format(log_filename) ]
52+ args += [ "--{}={}".format(key, value) for key, value in iteritems(wlargs) ]
53
54 widelands = subprocess.Popen(
55 args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
56@@ -84,21 +102,14 @@
57 line = widelands.stdout.readline()
58 if not line:
59 break
60- stdout_file.write(line)
61+ stdout_file.write(str(line))
62 stdout_file.flush()
63 widelands.communicate()
64- if platform.system() == "Windows":
65- win_stdout = self.path_to_widelands_binary.replace("widelands.exe","stdout.txt")
66- win_stderr = self.path_to_widelands_binary.replace("widelands.exe","stderr.txt")
67- with open(win_stdout,"r") as f:
68- for line in f:
69- stdout_file.write(line)
70- stdout_file.flush()
71- if (os.path.exists(win_stderr)):
72- with open(win_stderr,"r") as f:
73- for line in f:
74- stdout_file.write(line)
75- stdout_file.flush()
76+
77+ with open(log_filename,"r") as f:
78+ for line in f:
79+ stdout_file.write(line)
80+ stdout_file.flush()
81
82 self.widelands_returncode = widelands.returncode
83 return stdout_filename
84@@ -115,9 +126,9 @@
85 while not all(savegame_done.values()):
86 for savegame in sorted(savegame_done):
87 if not savegame_done[savegame]: break
88- out(" Loading savegame: %s ... " % savegame)
89+ out(" Loading savegame: {} ... ".format(savegame))
90 stdout_filename = self.run_widelands({ "loadgame": os.path.join(
91- self.run_dir, "save", "%s.wgf" % savegame) }, which_time)
92+ self.run_dir, "save", "{}.wgf".format(savegame))}, which_time)
93 which_time += 1
94 stdout = open(stdout_filename, "r").read()
95 for new_save in find_saves(stdout):
96@@ -127,17 +138,17 @@
97 self.verify_success(stdout, stdout_filename)
98
99 def verify_success(self, stdout, stdout_filename):
100- common_msg = "Analyze the files in %s to see why this test case failed. Stdout is\n %s\n\nstdout:\n%s" % (
101+ common_msg = "Analyze the files in {} to see why this test case failed. Stdout is\n {}\n\nstdout:\n{}".format(
102 self.run_dir, stdout_filename, stdout)
103 self.assertTrue(self.widelands_returncode == 0,
104- "Widelands exited abnormally. %s" % common_msg
105+ "Widelands exited abnormally. {}".format(common_msg)
106 )
107 self.assertTrue("All Tests passed" in stdout,
108- "Not all tests pass. %s." % common_msg
109+ "Not all tests pass. {}.".format(common_msg)
110 )
111 out("done.\n")
112 if self.keep_output_around:
113- out(" stdout: %s\n" % stdout_filename)
114+ out(" stdout: {}\n".format(stdout_filename))
115
116 def parse_args():
117 p = argparse.ArgumentParser(description=
118@@ -214,7 +225,7 @@
119 args = parse_args()
120
121 WidelandsTestCase.path_to_widelands_binary = args.binary
122- print "Using '%s' binary." % args.binary
123+ print("Using '{}' binary.".format(args.binary))
124 WidelandsTestCase.do_use_random_directory = not args.nonrandom
125 WidelandsTestCase.keep_output_around = args.keep_around
126
127@@ -222,7 +233,7 @@
128 if args.regexp:
129 all_files = [filename for filename in all_files if re.search(args.regexp, filename) ]
130
131- all_modules = [ "test.%s" % filename[:-3] for filename in all_files ]
132+ all_modules = [ "test.{}".format(filename[:-3]) for filename in all_files ]
133
134 suite = unittest.TestSuite()
135 discover_loadgame_tests(args.regexp, suite)

Subscribers

People subscribed via source and target branches

to status/vote changes: