<commit_before>import os
import tempfile
import unittest.mock

import pytest
import toml

from srm.config import Conf, GlobalConf, LocalConf


def test_config_not_exists():
    with tempfile.TemporaryDirectory() as d:
        c = Conf(d)
        assert not c.exists()


def test_config_exists():
    with tempfile.NamedTemporaryFile() as t:
        c = Conf(t.name)
        assert c.exists()


def test_load_create_with_path():
    with tempfile.TemporaryDirectory() as d:
        c = Conf(os.path.join(d, "a/b/file.txt"))
        c.load(create=True)
        assert c.exists()


def test_set_and_get_key():
    c = Conf('cofig')
    c['key'] = 10
    assert c['key'] == 10


def test_set_key_not_in_whitelist_raises_exception():
    c = Conf('cofig', valid_keys=['field_ok'])
    with pytest.raises(KeyError):
        c['field_bad'] = 10


def test_set_and_del_key():
    c = Conf('config')
    c['key'] = 10
    del c['key']
    with pytest.raises(KeyError):
        c['key']


def test_set_and_get_nested_key():
    c = Conf('config')
    c['s1.s2.key'] = 10
    assert c['s1.s2.key'] == 10


def test_set_and_del_nested_key():
    c = Conf('config')
    c['s1.s2.key'] = 10
    del c['s1.s2.key']
    with pytest.raises(KeyError):
        c['s1.s2.key']


def test_dump_config():
    with tempfile.NamedTemporaryFile() as t:
        c = Conf(t.name)
        c['key'] = 10
        c.dump()
        with open(t.name) as f:
            assert f.readline() == 'key = 10\n'


def test_dump_nested_config():
    with tempfile.NamedTemporaryFile() as t:
        c = Conf(t.name)
        c['s1.s2.key'] = 10
        assert 's1.s2.key' in c
        c.dump()
        with open(t.name) as f:
            assert f.readlines() == ['[s1.s2]\n', 'key = 10\n']


def test_load_config():
    with tempfile.NamedTemporaryFile() as t:
        with open(t.name, 'w+') as f:
            toml.dump({'key1': 1, 'key2': 2}, f)
        c = Conf(t.name)
        c.load()
        assert 'key2' in c
        assert c['key2'] == 2


def test_dump_local_config():
    with tempfile.NamedTemporaryFile() as t:
        c = LocalConf(t.name)
        c['key'] = 10
        c.dump()
        with open(t.name) as f:
            assert f.readline() == 'key = 10\n'


def test_load_local_config():
    with tempfile.NamedTemporaryFile() as t:
        with open(t.name, 'w+') as f:
            toml.dump({'key1': 1, 'key2': 2}, f)
        c = LocalConf(t.name)
        c.load()
        assert 'key2' in c
        assert c['key2'] == 2


@unittest.mock.patch('srm.config.GlobalConf', autospec=True)
def test_get_global_key_from_local_config(gconf_cls):
    with tempfile.NamedTemporaryFile() as global_path:
        # set a key/value into mock global config
        gconf = Conf(global_path.name)
        gconf_cls.return_value = gconf
        gconf['key'] = 10
        gconf.dump()
        # test
        with tempfile.NamedTemporaryFile() as local_path:
            c = LocalConf(local_path.name)
            c.load()
            assert 'key' in c
            assert c['key'] == 10<commit_msg>Uses sentinels in config testing<commit_after>import os
import tempfile
from unittest.mock import sentinel, patch

import pytest
import toml

from srm.config import Conf, GlobalConf, LocalConf

# value for testing set/get
V = sentinel.V

def test_config_not_exists():
    with tempfile.TemporaryDirectory() as d:
        c = Conf(d)
        assert not c.exists()


def test_config_exists():
    with tempfile.NamedTemporaryFile() as t:
        c = Conf(t.name)
        assert c.exists()


def test_load_create_with_path():
    with tempfile.TemporaryDirectory() as d:
        c = Conf(os.path.join(d, "a/b/file.txt"))
        c.load(create=True)
        assert c.exists()


def test_set_and_get_key():
    c = Conf('cofig')
    c['key'] = V
    assert c['key'] == V


def test_set_key_not_in_whitelist_raises_exception():
    c = Conf('cofig', valid_keys=['field_ok'])
    with pytest.raises(KeyError):
        c['field_bad'] = V


def test_set_and_del_key():
    c = Conf('config')
    c['key'] = V
    del c['key']
    with pytest.raises(KeyError):
        c['key']


def test_set_and_get_nested_key():
    c = Conf('config')
    c['s1.s2.key'] = V
    assert c['s1.s2.key'] == V


def test_set_and_del_nested_key():
    c = Conf('config')
    c['s1.s2.key'] = V
    del c['s1.s2.key']
    with pytest.raises(KeyError):
        c['s1.s2.key']


def test_dump_config():
    with tempfile.NamedTemporaryFile() as t:
        c = Conf(t.name)
        c['key'] = V
        c.dump()
        with open(t.name) as f:
            assert f.readline() == 'key = sentinel.V\n'


def test_dump_nested_config():
    with tempfile.NamedTemporaryFile() as t:
        c = Conf(t.name)
        c['s1.s2.key'] = V
        assert 's1.s2.key' in c
        c.dump()
        with open(t.name) as f:
            assert f.readlines() == ['[s1.s2]\n', 'key = sentinel.V\n']


def test_load_config():
    with tempfile.NamedTemporaryFile() as t:
        with open(t.name, 'w+') as f:
            toml.dump({'key1': 1, 'key2': 2}, f)
        c = Conf(t.name)
        c.load()
        assert 'key2' in c
        assert c['key2'] == 2


def test_dump_local_config():
    with tempfile.NamedTemporaryFile() as t:
        c = LocalConf(t.name)
        c['key'] = V
        c.dump()
        with open(t.name) as f:
            assert f.readline() == 'key = sentinel.V\n'


def test_load_local_config():
    with tempfile.NamedTemporaryFile() as t:
        with open(t.name, 'w+') as f:
            toml.dump({'key1': 1, 'key2': 2}, f)
        c = LocalConf(t.name)
        c.load()
        assert 'key2' in c
        assert c['key2'] == 2


@patch('srm.config.GlobalConf', autospec=True)
def test_get_global_key_from_local_config(gconf_cls):
    with tempfile.NamedTemporaryFile() as global_path:
        # set a key/value into mock global config
        gconf = Conf(global_path.name)
        gconf_cls.return_value = gconf
        gconf['key'] = 1
        gconf.dump()
        # test
        with tempfile.NamedTemporaryFile() as local_path:
            c = LocalConf(local_path.name)
            c.load()
            assert 'key' in c
            assert c['key'] == 1
{'fraction_non_alphanumeric': 0.14071324834597385, 'fraction_numerical': 0.014845893174116508, 'mean_word_length': 2.3412398921832884, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5860729', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2230, 'n_words': 510}