<commit_before># -*- coding: utf-8 -*-

"""Tests for the 'permissions' plugin.
"""
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)

import os
from mock import patch, Mock

from test._common import unittest
from test.helper import TestHelper
from beetsplug.permissions import (check_permissions,
                                   convert_perm,
                                   dirs_in_library)


class PermissionsPluginTest(unittest.TestCase, TestHelper):
    def setUp(self):
        self.setup_beets()
        self.load_plugins('permissions')

        self.config['permissions'] = {
            'file': 777,
            'dir': 777}

    def tearDown(self):
        self.teardown_beets()
        self.unload_plugins()

    def test_permissions_on_album_imported(self):
        self.do_test(True)

    def test_permissions_on_item_imported(self):
        self.config['import']['singletons'] = True
        self.do_test(True)

    @patch("os.chmod", Mock())
    def test_failing_to_set_permissions(self):
        self.do_test(False)

    def do_test(self, expectSuccess):
        self.importer = self.create_importer()
        self.importer.run()
        item = self.lib.items().get()

        exp_perms = {k: convert_perm(self.config['permissions'][k].get())
                     for k in ['file', 'dir']}

        self.assertPerms(item.path, convert_perm(644),
                         exp_perms['file'], expectSuccess)

        for path in dirs_in_library(self.lib.directory, item.path):
            self.assertPerms(path, convert_perm(755),
                             exp_perms['dir'], expectSuccess)

    def assertPerms(self, path, old_perms, new_perms, expectSuccess):
        for x in [(True, new_perms if expectSuccess else old_perms, '!='),
                  (False, old_perms if expectSuccess else new_perms, '==')]:
            self.assertEqual(x[0], check_permissions(path, x[1]),
                             msg='{} : {} {} {}'.format(
                path, oct(os.stat(path).st_mode), x[2], oct(x[1])))

    def test_convert_perm_from_string(self):
        self.assertEqual(convert_perm('10'), 8)
<commit_msg>Adjust test_permissions to handle different starting perms<commit_after># -*- coding: utf-8 -*-

"""Tests for the 'permissions' plugin.
"""
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)

import os
from mock import patch, Mock

from test._common import unittest
from test.helper import TestHelper
from beetsplug.permissions import (check_permissions,
                                   convert_perm,
                                   dirs_in_library)


class PermissionsPluginTest(unittest.TestCase, TestHelper):
    def setUp(self):
        self.setup_beets()
        self.load_plugins('permissions')

        self.config['permissions'] = {
            'file': 777,
            'dir': 777}

    def tearDown(self):
        self.teardown_beets()
        self.unload_plugins()

    def test_permissions_on_album_imported(self):
        self.do_test(True)

    def test_permissions_on_item_imported(self):
        self.config['import']['singletons'] = True
        self.do_test(True)

    @patch("os.chmod", Mock())
    def test_failing_to_set_permissions(self):
        self.do_test(False)

    def do_test(self, expectSuccess):
        def get_stat(v):
            return os.stat(
                os.path.join(self.temp_dir, 'import', *v)).st_mode & 0o777
        self.importer = self.create_importer()
        typs = ['file', 'dir']
        self.exp_perms = {
            True: {k: convert_perm(self.config['permissions'][k].get())
                   for k in typs},
            False: {k: get_stat(v)
                    for (k, v) in zip(typs, (('album 0', 'track 0.mp3'), ()))}}

        self.importer.run()
        item = self.lib.items().get()

        self.assertPerms(item.path, 'file', expectSuccess)

        for path in dirs_in_library(self.lib.directory, item.path):
            self.assertPerms(path, 'dir', expectSuccess)

    def assertPerms(self, path, typ, expectSuccess):
        for x in [(True, self.exp_perms[expectSuccess][typ], '!='),
                  (False, self.exp_perms[not expectSuccess][typ], '==')]:
            self.assertEqual(x[0], check_permissions(path, x[1]),
                             msg='{} : {} {} {}'.format(
                path, oct(os.stat(path).st_mode), x[2], oct(x[1])))

    def test_convert_perm_from_string(self):
        self.assertEqual(convert_perm('10'), 8)

{'fraction_non_alphanumeric': 0.12738017071569271, 'fraction_numerical': 0.008973517181002408, 'mean_word_length': 2.347985347985348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099533', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1365, 'n_words': 275}