@staticmethod def _RunBuildOutput(new_version_values={},
get_new_args=lambda old_args: old_args): """Parameterized helper method for running the main testable method in
version.py.
Keyword arguments:
new_version_values -- dict used to update _EXAMPLE_VERSION
get_new_args -- lambdafor updating _EXAMPLE_ANDROID_ARGS """
with mock.patch('version.FetchValuesFromFile') as \
fetch_values_from_file_mock:
def testFetchValuesFromFile(self): """It returns a dict in correct format - { <str>: <str> }, to verify
assumption of other tests that mock this function """
result = {}
version.FetchValuesFromFile(result, self._CHROME_VERSION_FILE)
for key, val in result.items():
self.assertIsInstance(key, str)
self.assertIsInstance(val, str)
def testBuildOutputAndroid(self): """Assert it gives includes assignments of expected variables"""
output = self._RunBuildOutput(
get_new_args=lambda args: self._EXAMPLE_ANDROID_ARGS)
contents = output['contents']
def testBuildOutputAndroidChromeArchInput(self): """Assert it raises an exception when using an invalid architecture input"""
new_args = _ReplaceArgs(self._EXAMPLE_ANDROID_ARGS, ['-a', 'foobar']) # Mock sys.stderr because argparse will print to stderr when we pass # the invalid '-a' value. with self.assertRaises(SystemExit) as cm, mock.patch('sys.stderr'):
self._RunBuildOutput(get_new_args=lambda args: new_args)
self.assertEqual(cm.exception.code, 2)
def testSetExecutable(self): """Assert that -x sets executable on POSIX and is harmless on Windows.""" with tempfile.TemporaryDirectory() as tmpdir:
in_file = os.path.join(tmpdir, "in")
out_file = os.path.join(tmpdir, "out") with open(in_file, "w") as f:
f.write("")
self.assertEqual(version.main(['-i', in_file, '-o', out_file, '-x']), 0)
# Whether lstat(out_file).st_mode has the executable bits set is # platform-specific. Therefore, test that out_file has the same # permissions that in_file would have after chmod(in_file, 0o755). # On Windows: both files will have 0o666. # On POSIX: both files will have 0o755.
os.chmod(in_file, 0o755) # On Windows, this sets in_file to 0o666.
self.assertEqual(os.lstat(in_file).st_mode, os.lstat(out_file).st_mode)
def testWriteIfChangedUpdateWhenContentChanged(self): """Assert it updates mtime of file when content is changed.""" with tempfile.TemporaryDirectory() as tmpdir:
file_name = os.path.join(tmpdir, "version.h")
old_contents = "old contents" with open(file_name, "w") as f:
f.write(old_contents)
os.chmod(file_name, 0o644)
mtime = os.lstat(file_name).st_mtime
time.sleep(0.1)
contents = "new contents"
version.WriteIfChanged(file_name, contents, 0o644) with open(file_name) as f:
self.assertEqual(contents, f.read())
self.assertNotEqual(mtime, os.lstat(file_name).st_mtime)
def testWriteIfChangedUpdateWhenModeChanged(self): """Assert it updates mtime of file when mode is changed.""" with tempfile.TemporaryDirectory() as tmpdir:
file_name = os.path.join(tmpdir, "version.h")
contents = "old contents" with open(file_name, "w") as f:
f.write(contents)
os.chmod(file_name, 0o644)
mtime = os.lstat(file_name).st_mtime
time.sleep(0.1)
version.WriteIfChanged(file_name, contents, 0o755) with open(file_name) as f:
self.assertEqual(contents, f.read())
self.assertNotEqual(mtime, os.lstat(file_name).st_mtime)
def testWriteIfChangedNoUpdate(self): """Assert it does not update mtime of file when nothing is changed.""" with tempfile.TemporaryDirectory() as tmpdir:
file_name = os.path.join(tmpdir, "version.h")
contents = "old contents" with open(file_name, "w") as f:
f.write(contents)
os.chmod(file_name, 0o644)
mtime = os.lstat(file_name).st_mtime
time.sleep(0.1)
version.WriteIfChanged(file_name, contents, 0o644) with open(file_name) as f:
self.assertEqual(contents, f.read())
self.assertEqual(mtime, os.lstat(file_name).st_mtime)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.