Skip to content
Snippets Groups Projects
  • Sybren A. Stüvel's avatar
    9e55ccd6
    Moved from requirements.txt to Pipfile · 9e55ccd6
    Sybren A. Stüvel authored
    This also means that we need a `tests/__init__.py` file; this file makes
    py.test undertand what is our sources directory, and thus allows them to
    `import flamenco_worker`. As a result, the test imports from tests/*.py
    need to change to relative imports.
    9e55ccd6
    History
    Moved from requirements.txt to Pipfile
    Sybren A. Stüvel authored
    This also means that we need a `tests/__init__.py` file; this file makes
    py.test undertand what is our sources directory, and thus allows them to
    `import flamenco_worker`. As a result, the test imports from tests/*.py
    need to change to relative imports.
test_commands_remove_tree.py 3.10 KiB
from pathlib import Path
import os

from .test_runner import AbstractCommandTest


class RemoveTreeTest(AbstractCommandTest):
    def setUp(self):
        super().setUp()

        from flamenco_worker.commands import RemoveTreeCommand
        import tempfile

        self.tmpdir = tempfile.TemporaryDirectory()
        self.tmppath = Path(self.tmpdir.name)

        self.cmd = RemoveTreeCommand(
            worker=self.fworker,
            task_id='12345',
            command_idx=0,
        )

    def tearDown(self):
        super().tearDown()
        self.tmpdir.cleanup()

    def test_validate_settings(self):
        self.assertIn('path', self.cmd.validate({'path': 12}))
        self.assertIn('path', self.cmd.validate({'path': ''}))
        self.assertIn('path', self.cmd.validate({}))
        self.assertFalse(self.cmd.validate({'path': '/some/path'}))

    def test_nonexistant_source(self):
        path = self.tmppath / 'nonexisting'
        task = self.cmd.run({'path': str(path)})
        ok = self.loop.run_until_complete(task)

        self.assertTrue(ok)
        self.assertFalse(path.exists())

    def test_source_file(self):
        path = self.tmppath / 'existing'
        path.touch()
        task = self.cmd.run({'path': str(path)})
        ok = self.loop.run_until_complete(task)

        self.assertTrue(ok)
        self.assertFalse(path.exists())

    def test_soure_dir_with_files(self):
        path = self.tmppath / 'dir'
        path.mkdir()
        (path / 'a.file').touch()
        (path / 'b.file').touch()
        (path / 'c.file').touch()

        task = self.cmd.run({'path': str(path)})
        ok = self.loop.run_until_complete(task)

        self.assertTrue(ok)
        self.assertFalse(path.exists())

    def test_soure_dir_with_files_and_dirs(self):
        path = self.tmppath / 'dir'
        path.mkdir()
        (path / 'subdir-a' / 'subsub-1').mkdir(parents=True)
        (path / 'subdir-a' / 'subsub-2').mkdir()
        (path / 'subdir-a' / 'subsub-3').mkdir()
        (path / 'subdir-b' / 'subsub-1').mkdir(parents=True)
        (path / 'subdir-c' / 'subsub-1').mkdir(parents=True)
        (path / 'subdir-c' / 'subsub-2').mkdir()
        (path / 'a.file').touch()
        (path / 'b.file').touch()
        (path / 'c.file').touch()

        (path / 'subdir-a' / 'subsub-1' / 'a.file').touch()
        (path / 'subdir-a' / 'subsub-1' / 'b.file').touch()
        (path / 'subdir-a' / 'subsub-2' / 'a.file').touch()
        (path / 'subdir-a' / 'subsub-2' / 'b.file').touch()
        (path / 'subdir-a' / 'subsub-3' / 'a.file').touch()
        (path / 'subdir-a' / 'subsub-3' / 'b.file').touch()
        (path / 'subdir-b' / 'subsub-1' / 'a.file').touch()
        (path / 'subdir-b' / 'subsub-1' / 'b.file').touch()
        (path / 'subdir-c' / 'subsub-1' / 'a.file').touch()
        (path / 'subdir-c' / 'subsub-1' / 'b.file').touch()
        (path / 'subdir-c' / 'subsub-2' / 'a.file').touch()
        (path / 'subdir-c' / 'subsub-2' / 'b.file').touch()

        task = self.cmd.run({'path': str(path)})
        ok = self.loop.run_until_complete(task)

        self.assertTrue(ok)
        self.assertFalse(path.exists())