def test_make_zipfile_no_zlib(self): patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError called = [] zipfile_class = zipfile.ZipFile def fake_zipfile(*a, **kw): if kw.get('compression', None) == zipfile.ZIP_STORED: called.append((a, kw)) return zipfile_class(*a, **kw) patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile) # create something to tar and compress tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with change_cwd(tmpdir): make_zipfile(base_name, 'dist') tarball = base_name + '.zip' self.assertEqual(called, [((tarball, "w"), { 'compression': zipfile.ZIP_STORED })]) self.assertTrue(os.path.exists(tarball)) with zipfile.ZipFile(tarball) as zf: self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
def test_make_zipfile_no_zlib(self): patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError called = [] zipfile_class = zipfile.ZipFile def fake_zipfile(*a, **kw): if kw.get('compression', None) == zipfile.ZIP_STORED: called.append((a, kw)) return zipfile_class(*a, **kw) patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile) # create something to tar and compress tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with change_cwd(tmpdir): make_zipfile(base_name, 'dist') tarball = base_name + '.zip' self.assertEqual(called, [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]) self.assertTrue(os.path.exists(tarball)) with zipfile.ZipFile(tarball) as zf: self.assertEqual(sorted(zf.namelist()), ['dist/file1', 'dist/file2', 'dist/sub/file3'])
def test_invalid_option(self): support.patch(self, sys, "argv", [sys.executable, "--invalid"]) with support.captured_stdout() as output: with self.assertRaises(SystemExit) as cm: mimetypes._main() self.assertIn("Usage: mimetypes.py", output.getvalue()) self.assertEqual(cm.exception.code, 1)
def test_make_zipfile_no_zlib(self): patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError called = [] zipfile_class = zipfile.ZipFile def fake_zipfile(*a, **kw): if kw.get('compression', None) == zipfile.ZIP_STORED: called.append((a, kw)) return zipfile_class(*a, **kw) patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile) # create something to tar and compress tmpdir, tmpdir2, base_name = self._create_files() make_zipfile(base_name, tmpdir) tarball = base_name + '.zip' self.assertEqual(called, [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]) self.assertTrue(os.path.exists(tarball))
def _test(self, meth, *, args=[URL], kw={}, options, arguments): """Given a web browser instance method name along with arguments and keywords for same (which defaults to the single argument URL), creates a browser instance from the class pointed to by self.browser, calls the indicated instance method with the indicated arguments, and compares the resulting options and arguments passed to Popen by the browser instance against the 'options' and 'args' lists. Options are compared in a position independent fashion, and the arguments are compared in sequence order to whatever is left over after removing the options. """ popen = PopenMock() support.patch(self, subprocess, 'Popen', popen) browser = self.browser_class(name=CMD_NAME) getattr(browser, meth)(*args, **kw) popen_args = subprocess.Popen.call_args[0][0] self.assertEqual(popen_args[0], CMD_NAME) popen_args.pop(0) for option in options: self.assertIn(option, popen_args) popen_args.pop(popen_args.index(option)) self.assertEqual(popen_args, arguments)
def _test_log(self, method, level=None): # logging.root has no handlers so basicConfig should be called called = [] old_basic_config = logging.basicConfig def my_basic_config(*a, **kw): old_basic_config() old_level = logging.root.level logging.root.setLevel(100) # avoid having messages in stderr self.addCleanup(logging.root.setLevel, old_level) called.append((a, kw)) patch(self, logging, 'basicConfig', my_basic_config) log_method = getattr(logging, method) if level is not None: log_method(level, "test me") else: log_method("test me") # basicConfig was called with no arguments self.assertEqual(called, [((), {})])
def _test_log(self, method, level=None): called = [] patch(self, logging, 'basicConfig', lambda *a, **kw: called.append( (a, kw))) recording = RecordingHandler() logging.root.addHandler(recording) log_method = getattr(logging, method) if level is not None: log_method(level, "test me: %r", recording) else: log_method("test me: %r", recording) self.assertEqual(len(recording.records), 1) record = recording.records[0] self.assertEqual(record.getMessage(), "test me: %r" % recording) expected_level = level if level is not None else getattr( logging, method.upper()) self.assertEqual(record.levelno, expected_level) # basicConfig was not called! self.assertEqual(called, [])
def mimetypes_cmd(self, *args, **kwargs): support.patch(self, sys, "argv", [sys.executable, *args]) with support.captured_stdout() as output: mimetypes._main() return output.getvalue().strip()