5 # This code is part of the LWN git data miner.
7 # Copyright 2009 Martin Nordholts <martinn@src.gnome.org>
9 # This file may be distributed under the terms of the GNU General
10 # Public License, version 2.
12 import unittest
, subprocess
, os
14 class GitdmTests(unittest
.TestCase
):
20 self
.srcdir
= os
.getcwd ()
21 self
.git_dir
= os
.path
.join (self
.srcdir
, "tests/testrepo")
22 if not os
.path
.exists (self
.git_dir
):
23 self
.fail ("'" + self
.git_dir
+ "' didn't exist, you probably "+
24 "didn't run the test with the source root as the working directory.")
28 # Makes sure that the statistics collected for the test repository
29 # is the expected statistics. Note that the test must be run with
30 # the working directory as the source root and with git in the
33 def testResultOutputRegressionTest(self
):
36 actual_results_path
= os
.path
.join (self
.srcdir
, "tests/actual-results.txt")
37 expected_results_path
= os
.path
.join (self
.srcdir
, "tests/expected-results.txt")
40 self
.runOutputFileRegressionTest (expected_results_path
,
42 ["-o", actual_results_path
])
46 # Does a regression test on the datelc (data line count) file
48 def testDateLineCountOutputRegressionTest(self
):
51 actual_datelc_path
= os
.path
.join (self
.srcdir
, "datelc")
52 expected_datelc_path
= os
.path
.join (self
.srcdir
, "tests/expected-datelc")
55 self
.runOutputFileRegressionTest (expected_datelc_path
,
61 # Run a test, passing path to file with expected output, path to
62 # file which will countain the actual output, and arguments to
63 # pass to gitdm. We both make sure the file where the result will
64 # be put when gitdm is run does not exist beforehand, and we clean
65 # up after we are done.
67 def runOutputFileRegressionTest(self
, expected_output_path
, actual_output_path
, arguments
):
69 # Make sure we can safely run the test
70 self
.ensureFileDoesNotExist (actual_output_path
)
74 self
.runGitdm (arguments
)
76 # Make sure we got the result we expected
77 self
.assertFilesEqual (expected_output_path
, actual_output_path
)
80 # Remove any file we created, also if test fails
81 self
.purgeFile (actual_output_path
)
85 # If passed file exists, delete it.
87 def purgeFile(self
, filename
):
88 if os
.path
.exists (filename
):
93 # Make sure the file does not exist so we don't risk overwriting
96 def ensureFileDoesNotExist(self
, filename
):
97 if os
.path
.exists (filename
):
98 self
.fail ("The file '" + filename
+ "' exists, failing "
99 "test to avoid overwriting file.")
103 # Run gitdm on the test repository with the passed arguments.
105 def runGitdm(self
, arguments
):
106 git_log_process
= subprocess
.Popen (["git", "--git-dir", self
.git_dir
, "log", "-p", "-M"],
107 stdout
=subprocess
.PIPE
)
108 gitdm_process
= subprocess
.Popen (["./gitdm"] + arguments
,
109 stdin
=git_log_process
.stdout
)
110 gitdm_process
.communicate ()
114 # Makes sure the files have the same content.
116 def assertFilesEqual(self
, file1
, file2
):
117 f
= open (file1
, 'r')
118 file1_contents
= f
.read ()
120 f
= open (file2
, 'r')
121 file2_contents
= f
.read ()
123 self
.assertEqual (file1_contents
, file2_contents
,
124 "The files '" + file1
+ "' and '" +
125 file2
+ "' were not equal!")
128 if __name__
== '__main__':