<commit_before># project
from checks import AgentCheck
from tests.checks.common import AgentCheckTest, load_check

class TestFileUnit(AgentCheckTest):
    CHECK_NAME = 'file'

    def assert_tags(self, expected_tags, present_tags):
        for tag in expected_tags:
            self.assertTrue(tag in present_tags)

    def test_present_success(self):
        conf = {
            'init_config': {},
            'instances': [
                {'path': __file__, 'expect': 'present'}
            ]
        }
        self.check = load_check('file', conf, {})
        self.check.check(conf['instances'][0])
        metrics = self.check.get_metrics()
        self.assertTrue(len(metrics) == 1)
        metric = metrics[0]
        self.assertTrue(metric[2] > 0)
        self.assert_tags(['expected_status:present', 'actual_status:present'], metric[3]['tags'])

        service_checks = self.check.get_service_checks()
        self.assertTrue(service_checks[0]['status'] == AgentCheck.OK)
        self.assert_tags(['expected_status:present', 'actual_status:present'], service_checks[0]['tags'])

    def test_glob_present_success(self):
        conf = {
            'init_config': {},
            'instances': [
                {'path': '/tmp/*', 'expect': 'present'}
            ]
        }
        self.check = load_check('file', conf, {})
        self.check.check(conf['instances'][0])
        metrics = self.check.get_metrics()
        self.assertTrue(len(metrics) == 1)
        metric = metrics[0]
        self.assertTrue(metric[2] > 0)
        self.assert_tags(['expected_status:present', 'actual_status:present'], metric[3]['tags'])

        service_checks = self.check.get_service_checks()
        self.assertTrue(service_checks[0]['status'] == AgentCheck.OK)
        self.assert_tags(['expected_status:present', 'actual_status:present'], service_checks[0]['tags'])

    def test_glob_present_failure(self):
        conf = {
            'init_config': {},
            'instances': [
                {'path': '/doesntexist/*', 'expect': 'present'}
            ]
        }
        self.check = load_check('file', conf, {})
        self.check.check(conf['instances'][0])

        service_checks = self.check.get_service_checks()
        self.assertTrue(service_checks[0]['status'] == AgentCheck.CRITICAL)
        self.assert_tags(['expected_status:present', 'actual_status:absent'], service_checks[0]['tags'])<commit_msg>Make temp files<commit_after>from tempfile import mkstemp, gettempdir

# project
from checks import AgentCheck
from tests.checks.common import AgentCheckTest, load_check

class TestFileUnit(AgentCheckTest):
    CHECK_NAME = 'file'

    def assert_tags(self, expected_tags, present_tags):
        for tag in expected_tags:
            self.assertTrue(tag in present_tags)

    def test_present_success(self):
        conf = {
            'init_config': {},
            'instances': [
                {'path': __file__, 'expect': 'present'}
            ]
        }
        self.check = load_check('file', conf, {})
        self.check.check(conf['instances'][0])
        metrics = self.check.get_metrics()
        self.assertTrue(len(metrics) == 1)
        metric = metrics[0]
        self.assertTrue(metric[2] > 0)
        self.assert_tags(['expected_status:present', 'actual_status:present'], metric[3]['tags'])

        service_checks = self.check.get_service_checks()
        self.assertTrue(service_checks[0]['status'] == AgentCheck.OK)
        self.assert_tags(['expected_status:present', 'actual_status:present'], service_checks[0]['tags'])

    def test_glob_present_success(self):
        # Make some temporary files, just in case
        mkstemp()
        mkstemp()

        conf = {
            'init_config': {},
            'instances': [
                {'path': gettempdir() + "/*", 'expect': 'present'}
            ]
        }
        self.check = load_check('file', conf, {})
        self.check.check(conf['instances'][0])
        metrics = self.check.get_metrics()
        self.assertTrue(len(metrics) == 1)
        metric = metrics[0]
        self.assertTrue(metric[2] > 0)
        self.assert_tags(['expected_status:present', 'actual_status:present'], metric[3]['tags'])

        service_checks = self.check.get_service_checks()
        self.assertTrue(service_checks[0]['status'] == AgentCheck.OK)
        self.assert_tags(['expected_status:present', 'actual_status:present'], service_checks[0]['tags'])

    def test_glob_present_failure(self):
        conf = {
            'init_config': {},
            'instances': [
                {'path': '/doesntexist/*', 'expect': 'present'}
            ]
        }
        self.check = load_check('file', conf, {})
        self.check.check(conf['instances'][0])

        service_checks = self.check.get_service_checks()
        self.assertTrue(service_checks[0]['status'] == AgentCheck.CRITICAL)
        self.assert_tags(['expected_status:present', 'actual_status:absent'], service_checks[0]['tags'])
{'fraction_non_alphanumeric': 0.1479325572059414, 'fraction_numerical': 0.0076274588518667205, 'mean_word_length': 2.766439909297052, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4996079', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1438, 'n_words': 230}