def wrapper(case, *args, **kwargs): test_file = FilePath(case.mktemp()) test_file.touch() test_file.chmod(0o000) permissions = test_file.getPermissions() test_file.chmod(0o777) if permissions != Permissions(0o000): raise SkipTest("Can't run test on filesystem with broken permissions.") return test_method(case, *args, **kwargs)
def _testNonDirectoryChildEntry(self): path = FilePath(self.mktemp()) self.failIf(path.exists()) path.touch() child = path.child("test_package").path plugins.__path__.append(child) try: plgs = list(plugin.getPlugins(plugin.ITestPlugin)) self.assertEqual(len(plgs), 1) finally: plugins.__path__.remove(child)
def test_missing(self): """ Invoking I{css2xslfo} with a missing input raises an L{ExternalProcessError}. """ def checkException(e): self.assertIn('(No such file or directory)', e.stderr) outputPath = FilePath(self.mktemp()) outputPath.touch() d = css2xslfo(self.dataPath.child('missing.html'), outputPath) d = self.assertFailure(d, ExternalProcessError) d.addCallback(checkException) return d
def test_nonDirectoryChildEntry(self): """ Test that getCache skips over any entries in a plugin package's C{__path__} which refer to children of paths which are not directories. """ path = FilePath(self.mktemp()) self.assertFalse(path.exists()) path.touch() child = path.child("test_package").path self.module.__path__.append(child) try: plgs = list(plugin.getPlugins(ITestPlugin, self.module)) self.assertEqual(len(plgs), 1) finally: self.module.__path__.remove(child)
def test_nonDirectoryChildEntry(self): """ Test that getCache skips over any entries in a plugin package's C{__path__} which refer to children of paths which are not directories. """ path = FilePath(self.mktemp()) self.failIf(path.exists()) path.touch() child = path.child("test_package").path self.module.__path__.append(child) try: plgs = list(plugin.getPlugins(ITestPlugin, self.module)) self.assertEqual(len(plgs), 1) finally: self.module.__path__.remove(child)
def test_broken(self): """ Invoking I{css2xslfo} with a broken input raises an L{ExternalProcessError}. """ def checkException(e): self.assertIn( 'element type "div" must be terminated by the matching end-tag', e.stderr) outputPath = FilePath(self.mktemp()) outputPath.touch() d = css2xslfo(self.dataPath.child('broken.html'), outputPath) d = self.assertFailure(d, ExternalProcessError) d.addCallback(checkException) return d
def test_runnerDebuggerDefaultsToPdb(self): """ Trial uses pdb if no debugger is specified by `--debugger` """ self.parseOptions(['--debug', 'twisted.trial.test.sample']) pdbrcFile = FilePath("pdbrc") pdbrcFile.touch() self.runcall_called = False def runcall(pdb, suite, result): self.runcall_called = True self.patch(pdb.Pdb, "runcall", runcall) self.runSampleSuite(self.getRunner()) self.assertTrue(self.runcall_called)
class OptionsTests(unittest.TestCase): """ Tests for L{txghbot._api.Options} """ def setUp(self): self.secretFilePath = self.mktemp() self.secretFile = FilePath(self.secretFilePath) self.secretFromPathOption = "--secret-from-path={}".format( self.secretFilePath) self.stringEnviron = {} self.bytesEnviron = {} self.osModuleFake = _FakeOSModule.fromdicts(self.stringEnviron, self.bytesEnviron) self.config = Options(self.osModuleFake) def tearDown(self): if self.secretFile.exists(): self.secretFile.remove() def secretFromEnvironmentOption(self, variable): """ Generate the secret from environment variable command line option. @type variable: L{str} @param variable: The variable name @return: The formatted option containing the path. @rtype: L{str} """ return "--secret-from-env={}".format(variable) def test_retrieveSecretFromEmptyPath(self): """ An empty secret file raises an L{OSError} """ self.secretFile.touch() exc = self.assertRaises( UsageError, self.config.parseOptions, [self.secretFromPathOption], ) self.assertIn("empty", str(exc).lower()) def test_retrieveSecretFromMissingPath(self): """ An missing secret file raises an L{IOError} """ self.assertRaises( IOError, self.config.parseOptions, [self.secretFromPathOption], ) def test_retrieveSecretFromPath(self): """ A newline-stripped secret is returned. Internal whitespace is preserved. """ with self.secretFile.open('wb') as f: f.write(b" this is a secret \r\n") self.config.parseOptions([self.secretFromPathOption]) self.assertEqual(self.config["secret"], b" this is a secret ") def test_retrieveSecretFromEmptyEnvironment(self): """ A L{UsageError} is raised when attempting to retrieve a secret from an environment that doesn't contain the provided variable. """ self.assertRaises(UsageError, self.config.parseOptions, [self.secretFromEnvironmentOption("MISSING")]) def test_retrieveSecretFromEnvironment(self): """ The secret is retrieved as bytes from the process' environment. """ self.bytesEnviron[b"SECRET"] = b"a secret" self.stringEnviron["SECRET"] = "a secret" self.config.parseOptions([self.secretFromEnvironmentOption("SECRET")]) self.assertEqual(self.config["secret"], b"a secret") def test_missingSecret(self): """ Omitting both a secret path and a secret environment variable name results in a L{UsageError}. """ self.assertRaises(UsageError, self.config.parseOptions, []) def test_bothSecrets(self): """ Including both a secret path and a secret environment variable name results in a L{UsageError}. """ self.assertRaises(UsageError, self.config.parseOptions, [self.secretFromPathOption, self.secretFromEnvironmentOption("REDUNDANT")])