Exemplo n.º 1
0
    def test_mkdtemp_setup_teardown(self):
        def faux_cleaner():
            pass

        DIR1, DIR2 = 'fake_dir1__does_not_exist', 'fake_dir2__does_not_exist'
        self._mox.StubOutWithMock(atexit, 'register')
        self._mox.StubOutWithMock(os, 'getpid')
        self._mox.StubOutWithMock(tempfile, 'mkdtemp')
        self._mox.StubOutWithMock(dirutil, 'safe_rmtree')
        atexit.register(faux_cleaner)  # Ensure only called once.
        tempfile.mkdtemp(dir='1').AndReturn(DIR1)
        tempfile.mkdtemp(dir='2').AndReturn(DIR2)
        os.getpid().MultipleTimes().AndReturn('unicorn')
        dirutil.safe_rmtree(DIR1)
        dirutil.safe_rmtree(DIR2)
        # Make sure other "pids" are not cleaned.
        dirutil._MKDTEMP_DIRS['fluffypants'].add('yoyo')

        try:
            self._mox.ReplayAll()
            self.assertEquals(
                DIR1, dirutil.safe_mkdtemp(dir='1', cleaner=faux_cleaner))
            self.assertEquals(
                DIR2, dirutil.safe_mkdtemp(dir='2', cleaner=faux_cleaner))
            self.assertIn('unicorn', dirutil._MKDTEMP_DIRS)
            self.assertEquals({DIR1, DIR2}, dirutil._MKDTEMP_DIRS['unicorn'])
            dirutil._mkdtemp_atexit_cleaner()
            self.assertNotIn('unicorn', dirutil._MKDTEMP_DIRS)
            self.assertEquals({'yoyo'}, dirutil._MKDTEMP_DIRS['fluffypants'])
        finally:
            dirutil._MKDTEMP_DIRS.pop('unicorn', None)
            dirutil._MKDTEMP_DIRS.pop('fluffypants', None)
            dirutil._mkdtemp_unregister_cleaner()

        self._mox.VerifyAll()
Exemplo n.º 2
0
    def test_mkdtemp_setup_teardown(self, tempfile_mkdtemp,
                                    dirutil_safe_rmtree, os_getpid,
                                    atexit_register):
        def faux_cleaner():
            pass

        DIR1, DIR2 = 'fake_dir1__does_not_exist', 'fake_dir2__does_not_exist'

        # Make sure other "pids" are not cleaned.
        dirutil._MKDTEMP_DIRS['fluffypants'].add('yoyo')

        tempfile_mkdtemp.side_effect = (DIR1, DIR2)
        os_getpid.return_value = 'unicorn'
        try:
            self.assertEquals(
                DIR1, dirutil.safe_mkdtemp(dir='1', cleaner=faux_cleaner))
            self.assertEquals(
                DIR2, dirutil.safe_mkdtemp(dir='2', cleaner=faux_cleaner))
            self.assertIn('unicorn', dirutil._MKDTEMP_DIRS)
            self.assertEquals({DIR1, DIR2}, dirutil._MKDTEMP_DIRS['unicorn'])
            dirutil._mkdtemp_atexit_cleaner()
            self.assertNotIn('unicorn', dirutil._MKDTEMP_DIRS)
            self.assertEquals({'yoyo'}, dirutil._MKDTEMP_DIRS['fluffypants'])
        finally:
            dirutil._MKDTEMP_DIRS.pop('unicorn', None)
            dirutil._MKDTEMP_DIRS.pop('fluffypants', None)
            dirutil._mkdtemp_unregister_cleaner()

        atexit_register.assert_called_once_with(faux_cleaner)
        self.assertTrue(os_getpid.called)
        self.assertEqual(
            [mock.call(dir='1'), mock.call(dir='2')],
            tempfile_mkdtemp.mock_calls)
        self.assertEqual([mock.call(DIR1), mock.call(DIR2)],
                         dirutil_safe_rmtree.mock_calls)
Exemplo n.º 3
0
  def test_mkdtemp_setup_teardown(self):
    def faux_cleaner():
      pass

    DIR1, DIR2 = 'fake_dir1__does_not_exist', 'fake_dir2__does_not_exist'
    self._mox.StubOutWithMock(atexit, 'register')
    self._mox.StubOutWithMock(os, 'getpid')
    self._mox.StubOutWithMock(tempfile, 'mkdtemp')
    self._mox.StubOutWithMock(dirutil, 'safe_rmtree')
    atexit.register(faux_cleaner)  # Ensure only called once.
    tempfile.mkdtemp(dir='1').AndReturn(DIR1)
    tempfile.mkdtemp(dir='2').AndReturn(DIR2)
    os.getpid().MultipleTimes().AndReturn('unicorn')
    dirutil.safe_rmtree(DIR1)
    dirutil.safe_rmtree(DIR2)
    # Make sure other "pids" are not cleaned.
    dirutil._MKDTEMP_DIRS['fluffypants'].add('yoyo')

    try:
      self._mox.ReplayAll()
      self.assertEquals(DIR1, dirutil.safe_mkdtemp(dir='1', cleaner=faux_cleaner))
      self.assertEquals(DIR2, dirutil.safe_mkdtemp(dir='2', cleaner=faux_cleaner))
      self.assertIn('unicorn', dirutil._MKDTEMP_DIRS)
      self.assertEquals({DIR1, DIR2}, dirutil._MKDTEMP_DIRS['unicorn'])
      dirutil._mkdtemp_atexit_cleaner()
      self.assertNotIn('unicorn', dirutil._MKDTEMP_DIRS)
      self.assertEquals({'yoyo'}, dirutil._MKDTEMP_DIRS['fluffypants'])
    finally:
      dirutil._MKDTEMP_DIRS.pop('unicorn', None)
      dirutil._MKDTEMP_DIRS.pop('fluffypants', None)
      dirutil._mkdtemp_unregister_cleaner()

    self._mox.VerifyAll()
Exemplo n.º 4
0
  def test_mkdtemp_setup_teardown(self,
                                  tempfile_mkdtemp,
                                  dirutil_safe_rmtree,
                                  os_getpid,
                                  atexit_register):
    def faux_cleaner():
      pass

    DIR1, DIR2 = 'fake_dir1__does_not_exist', 'fake_dir2__does_not_exist'

    # Make sure other "pids" are not cleaned.
    dirutil._MKDTEMP_DIRS['fluffypants'].add('yoyo')

    tempfile_mkdtemp.side_effect = (DIR1, DIR2)
    os_getpid.return_value = 'unicorn'
    try:
      self.assertEquals(DIR1, dirutil.safe_mkdtemp(dir='1', cleaner=faux_cleaner))
      self.assertEquals(DIR2, dirutil.safe_mkdtemp(dir='2', cleaner=faux_cleaner))
      self.assertIn('unicorn', dirutil._MKDTEMP_DIRS)
      self.assertEquals({DIR1, DIR2}, dirutil._MKDTEMP_DIRS['unicorn'])
      dirutil._mkdtemp_atexit_cleaner()
      self.assertNotIn('unicorn', dirutil._MKDTEMP_DIRS)
      self.assertEquals({'yoyo'}, dirutil._MKDTEMP_DIRS['fluffypants'])
    finally:
      dirutil._MKDTEMP_DIRS.pop('unicorn', None)
      dirutil._MKDTEMP_DIRS.pop('fluffypants', None)
      dirutil._mkdtemp_unregister_cleaner()

    atexit_register.assert_called_once_with(faux_cleaner)
    self.assertTrue(os_getpid.called)
    self.assertEqual([mock.call(dir='1'), mock.call(dir='2')], tempfile_mkdtemp.mock_calls)
    self.assertEqual([mock.call(DIR1), mock.call(DIR2)], dirutil_safe_rmtree.mock_calls)
Exemplo n.º 5
0
    def test_mkdtemp_setup_teardown(
        self, tempfile_mkdtemp, dirutil_safe_rmtree, os_getpid, atexit_register
    ):
        def faux_cleaner():
            pass

        DIR1, DIR2 = "fake_dir1__does_not_exist", "fake_dir2__does_not_exist"

        # Make sure other "pids" are not cleaned.
        dirutil._MKDTEMP_DIRS["fluffypants"].add("yoyo")

        tempfile_mkdtemp.side_effect = (DIR1, DIR2)
        os_getpid.return_value = "unicorn"
        try:
            assert DIR1 == dirutil.safe_mkdtemp(dir="1", cleaner=faux_cleaner)
            assert DIR2 == dirutil.safe_mkdtemp(dir="2", cleaner=faux_cleaner)
            assert "unicorn" in dirutil._MKDTEMP_DIRS
            assert {DIR1, DIR2} == dirutil._MKDTEMP_DIRS["unicorn"]
            dirutil._mkdtemp_atexit_cleaner()
            assert "unicorn" not in dirutil._MKDTEMP_DIRS
            assert {"yoyo"} == dirutil._MKDTEMP_DIRS["fluffypants"]
        finally:
            dirutil._MKDTEMP_DIRS.pop("unicorn", None)
            dirutil._MKDTEMP_DIRS.pop("fluffypants", None)
            dirutil._mkdtemp_unregister_cleaner()

        atexit_register.assert_called_once_with(faux_cleaner)
        assert os_getpid.called
        assert [
            unittest.mock.call(dir="1"),
            unittest.mock.call(dir="2"),
        ] == tempfile_mkdtemp.mock_calls
        assert sorted([unittest.mock.call(DIR1), unittest.mock.call(DIR2)]) == sorted(
            dirutil_safe_rmtree.mock_calls
        )
Exemplo n.º 6
0
    def test_mkdtemp_setup_teardown(self):
        def faux_cleaner():
            pass

        DIR1, DIR2 = "fake_dir1__does_not_exist", "fake_dir2__does_not_exist"
        self._mox.StubOutWithMock(atexit, "register")
        self._mox.StubOutWithMock(os, "getpid")
        self._mox.StubOutWithMock(tempfile, "mkdtemp")
        self._mox.StubOutWithMock(dirutil, "safe_rmtree")
        atexit.register(faux_cleaner)  # Ensure only called once.
        tempfile.mkdtemp(dir="1").AndReturn(DIR1)
        tempfile.mkdtemp(dir="2").AndReturn(DIR2)
        os.getpid().MultipleTimes().AndReturn("unicorn")
        dirutil.safe_rmtree(DIR1)
        dirutil.safe_rmtree(DIR2)
        # Make sure other "pids" are not cleaned.
        dirutil._MKDTEMP_DIRS["fluffypants"].add("yoyo")

        try:
            self._mox.ReplayAll()
            self.assertEquals(DIR1, dirutil.safe_mkdtemp(dir="1", cleaner=faux_cleaner))
            self.assertEquals(DIR2, dirutil.safe_mkdtemp(dir="2", cleaner=faux_cleaner))
            self.assertIn("unicorn", dirutil._MKDTEMP_DIRS)
            self.assertEquals({DIR1, DIR2}, dirutil._MKDTEMP_DIRS["unicorn"])
            dirutil._mkdtemp_atexit_cleaner()
            self.assertNotIn("unicorn", dirutil._MKDTEMP_DIRS)
            self.assertEquals({"yoyo"}, dirutil._MKDTEMP_DIRS["fluffypants"])
        finally:
            dirutil._MKDTEMP_DIRS.pop("unicorn", None)
            dirutil._MKDTEMP_DIRS.pop("fluffypants", None)
            dirutil._mkdtemp_unregister_cleaner()

        self._mox.VerifyAll()
Exemplo n.º 7
0
    def test_mkdtemp_setup_teardown(
        self, tempfile_mkdtemp, dirutil_safe_rmtree, os_getpid, atexit_register
    ):
        def faux_cleaner():
            pass

        DIR1, DIR2 = "fake_dir1__does_not_exist", "fake_dir2__does_not_exist"

        # Make sure other "pids" are not cleaned.
        dirutil._MKDTEMP_DIRS["fluffypants"].add("yoyo")

        tempfile_mkdtemp.side_effect = (DIR1, DIR2)
        os_getpid.return_value = "unicorn"
        try:
            self.assertEqual(DIR1, dirutil.safe_mkdtemp(dir="1", cleaner=faux_cleaner))
            self.assertEqual(DIR2, dirutil.safe_mkdtemp(dir="2", cleaner=faux_cleaner))
            self.assertIn("unicorn", dirutil._MKDTEMP_DIRS)
            self.assertEqual({DIR1, DIR2}, dirutil._MKDTEMP_DIRS["unicorn"])
            dirutil._mkdtemp_atexit_cleaner()
            self.assertNotIn("unicorn", dirutil._MKDTEMP_DIRS)
            self.assertEqual({"yoyo"}, dirutil._MKDTEMP_DIRS["fluffypants"])
        finally:
            dirutil._MKDTEMP_DIRS.pop("unicorn", None)
            dirutil._MKDTEMP_DIRS.pop("fluffypants", None)
            dirutil._mkdtemp_unregister_cleaner()

        atexit_register.assert_called_once_with(faux_cleaner)
        self.assertTrue(os_getpid.called)
        self.assertEqual(
            [unittest.mock.call(dir="1"), unittest.mock.call(dir="2")], tempfile_mkdtemp.mock_calls
        )
        self.assertEqual(
            sorted([unittest.mock.call(DIR1), unittest.mock.call(DIR2)]),
            sorted(dirutil_safe_rmtree.mock_calls),
        )