def test_unpack_release_with_no_removal(self):
        """Test unpack of release with no remove activated."""
        # Unpack releases in search dir without removal
        for release in ("Release-Group", "Release.with.subs-Group"):
            self.copy_test_directory_to_search_dir(release)

        release_unpacker = ReleaseUnpacker(self.search_dir,
                                           self.tmp_dir,
                                           self.unpack_dir,
                                           no_remove=True)
        release_unpacker.unpack_release_dir_rars()

        # Validate extract of releases and subs
        self.assertEqual(
            Path(self.unpack_dir).listdir(),
            [
                Path(self.unpack_dir, "Release-Group.mkv"),
                Path(self.unpack_dir, "Release.with.subs-Group.idx"),
                Path(self.unpack_dir, "Release.with.subs-Group.mkv"),
                Path(self.unpack_dir, "Release.with.subs-Group.sub"),
            ],
        )

        # Make sure the unpacked subs rar file is removed
        self.assertFalse(
            Path(
                self.search_dir,
                "Release.with.subs-Group/Subs/subs_inside_rar.rar",
            ).exists())

        # Make sure the release dirs are not removed
        self.assertTrue(Path(self.search_dir, "Release-Group").exists())
        self.assertTrue(
            Path(self.search_dir, "Release.with.subs-Group").exists())
    def test_repr(self):
        """Test object string representation."""
        release_unpacker = ReleaseUnpacker(self.search_dir, self.tmp_dir,
                                           self.unpack_dir)

        self.assertEqual(
            release_unpacker.__repr__(),
            "<ReleaseUnpacker: {} ({}) ({})>".format(self.search_dir,
                                                     self.tmp_dir,
                                                     self.unpack_dir),
        )
    def test_scan_rars_single_release(self):
        """Test scanning of RAR files for a single release."""
        self.copy_test_directory_to_search_dir("Release-Group")
        release_unpacker = ReleaseUnpacker(self.search_dir, self.tmp_dir,
                                           self.unpack_dir)

        self.assertEqual(
            release_unpacker.scan_rars(),
            [
                Path(self.search_dir, "Release-Group/rar_file.rar"),
            ],
        )
    def test_tmp_dir_does_not_exist(self):
        """Test non existing tmp dir raises exception."""
        tmp_dir = Path("/non_existing_tmp_dir")

        with self.assertRaises(ReleaseUnpackerError) as cm:
            ReleaseUnpacker(self.search_dir, tmp_dir, self.unpack_dir)

        self.assertEqual(str(cm.exception),
                         "Tmp dir {} doesn't exist".format(tmp_dir))
    def test_unpack_release_with_unpack_time(self):
        """Test unpack of releases with unpack time mocked for logging."""
        self.copy_test_directory_to_search_dir("Release-Group")

        release_unpacker = ReleaseUnpacker(self.search_dir, self.tmp_dir,
                                           self.unpack_dir)

        with self.assertLogs(self.LOGGER_NAME, level="INFO") as cm:
            # Mock ago.human() to set a static unpack time
            with mock.patch("releaseunpacker.releaseunpacker.human",
                            return_value="1 min"):
                release_unpacker.unpack_release_dir_rars()

        self.assertIn(
            "INFO:releaseunpacker.releaseunpacker:Release-Group.mkv "
            "unpack done, 1 min",
            cm.output,
        )
    def test_unpack_dir_not_a_dir(self):
        """Test unpack dir is not a directory raises exception."""
        self.copy_test_file_to_unpack_dir("file_not_a_dir")
        unpack_dir = Path(self.unpack_dir, "file_not_a_dir")

        with self.assertRaises(ReleaseUnpackerError) as cm:
            ReleaseUnpacker(self.search_dir, self.tmp_dir, unpack_dir)

        self.assertEqual(str(cm.exception),
                         "Unpack dir {} is not a dir".format(unpack_dir))
    def test_scan_rars_multiple_releases(self):
        """Test scanning of RAR files with multiple releases in search dir."""
        for release in ("Release-Group", "Release.with.subs-Group"):
            self.copy_test_directory_to_search_dir(release)

        release_unpacker = ReleaseUnpacker(self.search_dir, self.tmp_dir,
                                           self.unpack_dir)

        self.assertEqual(
            release_unpacker.scan_rars(),
            [
                Path(self.search_dir, "Release-Group/rar_file.rar"),
                Path(
                    self.search_dir,
                    "Release.with.subs-Group/release.with.subs-group.rar",
                ),
                Path(
                    self.search_dir,
                    "Release.with.subs-Group/Subs/subs.rar",
                ),
            ],
        )
    def test_unpack_release_dir_rars_no_rars_found(self):
        """Test unpack release dir without RAR files."""
        release_unpacker = ReleaseUnpacker(self.search_dir, self.tmp_dir,
                                           self.unpack_dir)

        self.assertFalse(release_unpacker.unpack_release_dir_rars())