<commit_before># No shebang line, this module is meant to be imported
#
# Copyright 2014 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser as _ArgumentParser
from collections import namedtuple
from functools import partial

from pyfarm.agent.entrypoints.argtypes import (
    assert_instance, ip, port, integer, direxists, number, enum)
from pyfarm.agent.testutil import TestCase

DummyArgs = namedtuple("DummyArgs", ["uid"])


class ErrorCapturingParser(_ArgumentParser):
    def __init__(self, *args, **kwargs):
        super(ErrorCapturingParser, self).__init__(*args, **kwargs)
        self.errors = []

    def error(self, message):
        self.errors.append(message)


class BaseTestArgTypes(TestCase):
    def setUp(self):
        TestCase.setUp(self)
        self.args = None
        self.parser = ErrorCapturingParser()


class TestAssertInstance(TestCase):
    def test_instance_set(self):
        @assert_instance
        def function(instance=None):
            pass

        self.assertRaises(AssertionError, lambda: function(instance=None))

    def test_has_args_and_parser(self):
        @assert_instance
        def function(instance=None):
            return True

        self.parser = ErrorCapturingParser()
        self.args = self.parser.parse_args()
        args = self.args
        parser = self.parser
        del self.args
        del self.parser
        self.assertRaises(AssertionError, lambda: function(instance=self))
        self.parser = parser
        self.assertRaises(AssertionError, lambda: function(instance=self))
        self.args = args
        self.assertTrue(function(instance=self))


class TestIp(BaseTestArgTypes):
    def setUp(self):
        BaseTestArgTypes.setUp(self)
        self.parser.add_argument("--ip", type=partial(ip, instance=self))

    def test_valid(self):
        self.args = self.parser.parse_args(["--ip", "127.0.0.1"])
        self.assertEqual(self.args.ip, "127.0.0.1")

    def test_invalid(self):
        self.args = self.parser.parse_args(["--ip", "!"])
        self.assertEqual(self.parser.errors, ["! is not a valid ip address"])


class TestPort(BaseTestArgTypes):
    def setUp(self):
        BaseTestArgTypes.setUp(self)
        self.parser.add_argument("--port", type=partial(port, instance=self))
        self.parser.add_argument(
            "--uid", type=partial(integer, instance=self, min_=0))

    def test_valid_non_root_min(self):
        self.args = DummyArgs(uid=1000)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "49152"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 49152)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "49151"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 49152-65535"])

    def test_valid_non_root_max(self):
        self.args = DummyArgs(uid=1000)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "65535"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 65535)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "65536"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 49152-65535"])

    def test_valid_root_min(self):
        self.args = DummyArgs(uid=0)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "1"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 1)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "0"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 1-65535"])

    def test_valid_root_max(self):
        self.args = DummyArgs(uid=0)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "65535"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 65535)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "65536"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 1-65535"])

    def test_port_not_a_number(self):
        self.args = DummyArgs(uid=0)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "!"])
        self.assertEqual(
            self.parser.errors, ["failed to convert --port to a number"])


class TestDirectory(BaseTestArgTypes):
    def setUp(self):
        BaseTestArgTypes.setUp(self)
        self.parser.add_argument(
            "--dir", type=partial(direxists, instance=self, flag="dir"))

    def test_is_file_not_directory(self):
        path = self.create_test_file()
        self.args = self.parser.parse_args(["--dir", path])
        self.assertEqual(
            self.parser.errors,
            ["--dir, path does not exist or is not a directory: %s" % path])

    def test_directory_exists(self):<commit_msg>fixing test assumptions<commit_after># No shebang line, this module is meant to be imported
#
# Copyright 2014 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser as _ArgumentParser
from collections import namedtuple
from functools import partial

from pyfarm.agent.entrypoints.argtypes import (
    assert_instance, ip, port, integer, direxists, number, enum)
from pyfarm.agent.testutil import TestCase

DummyArgs = namedtuple("DummyArgs", ["uid"])


class ErrorCapturingParser(_ArgumentParser):
    def __init__(self, *args, **kwargs):
        super(ErrorCapturingParser, self).__init__(*args, **kwargs)
        self.errors = []

    def error(self, message):
        self.errors.append(message)


class BaseTestArgTypes(TestCase):
    def setUp(self):
        TestCase.setUp(self)
        self.args = None
        self.parser = ErrorCapturingParser()


class TestAssertInstance(TestCase):
    def test_instance_set(self):
        @assert_instance
        def function(instance=None):
            pass

        self.assertRaises(AssertionError, lambda: function(instance=None))

    def test_has_args_and_parser(self):
        @assert_instance
        def function(instance=None):
            return True

        self.parser = ErrorCapturingParser()
        self.args = self.parser.parse_args()
        args = self.args
        parser = self.parser
        del self.args
        del self.parser
        self.assertRaises(AssertionError, lambda: function(instance=self))
        self.parser = parser
        self.assertRaises(AssertionError, lambda: function(instance=self))
        self.args = args
        self.assertTrue(function(instance=self))


class TestIp(BaseTestArgTypes):
    def setUp(self):
        BaseTestArgTypes.setUp(self)
        self.parser.add_argument("--ip", type=partial(ip, instance=self))

    def test_valid(self):
        self.args = self.parser.parse_args(["--ip", "127.0.0.1"])
        self.assertEqual(self.args.ip, "127.0.0.1")

    def test_invalid(self):
        self.args = self.parser.parse_args(["--ip", "!"])
        self.assertEqual(self.parser.errors, ["! is not a valid ip address"])


class TestPort(BaseTestArgTypes):
    def setUp(self):
        BaseTestArgTypes.setUp(self)
        self.parser.add_argument("--port", type=partial(port, instance=self))
        self.parser.add_argument(
            "--uid", type=partial(integer, instance=self, min_=0))

    def test_valid_non_root_min(self):
        self.args = DummyArgs(uid=1000)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "49152"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 49152)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "49151"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 49152 to 65535"])

    def test_valid_non_root_max(self):
        self.args = DummyArgs(uid=1000)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "65535"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 65535)
        self.args = self.parser.parse_args(["--uid", "1000", "--port", "65536"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 49152 to 65535"])

    def test_valid_root_min(self):
        self.args = DummyArgs(uid=0)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "1"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 1)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "0"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 1 to 65535"])

    def test_valid_root_max(self):
        self.args = DummyArgs(uid=0)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "65535"])
        self.assertEqual(self.parser.errors, [])
        self.assertEqual(self.args.port, 65535)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "65536"])
        self.assertEqual(
            self.parser.errors, ["valid port range is 1 to 65535"])

    def test_port_not_a_number(self):
        self.args = DummyArgs(uid=0)
        self.args = self.parser.parse_args(["--uid", "0", "--port", "!"])
        self.assertEqual(
            self.parser.errors, ["failed to convert --port to a number"])


class TestDirectory(BaseTestArgTypes):
    def setUp(self):
        BaseTestArgTypes.setUp(self)
        self.parser.add_argument(
            "--dir", type=partial(direxists, instance=self, flag="dir"))

    def test_is_file_not_directory(self):
        path = self.create_test_file()
        self.args = self.parser.parse_args(["--dir", path])
        self.assertEqual(
            self.parser.errors,
            ["--dir, path does not exist or is not a directory: %s" % path])

    def test_directory_exists(self):
{'fraction_non_alphanumeric': 0.1357910337161912, 'fraction_numerical': 0.024638755094479436, 'mean_word_length': 2.944830105955426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '244716', 'n_tokens_mistral': 3420, 'n_tokens_neox': 3191, 'n_words': 753}