<commit_before>"""
Integration tests for roamer
"""

import os
from os.path import expanduser
import shutil
import unittest
import time
from tests.mock_session import MockSession
BASE_ROAMER_PATH = os.environ.get('ROAMER_DATA_PATH') or expanduser('~/.roamer-data/')
os.environ["ROAMER_DATA_PATH"] = expanduser(os.path.join(BASE_ROAMER_PATH, 'tmp/test/.roamer-data'))
from roamer.constant import TEST_DIR, ROAMER_DATA_PATH, TRASH_DIR # pylint: disable=wrong-import-position

HELLO_DIR = os.path.join(TEST_DIR, 'hello/')
DOC_DIR = os.path.join(TEST_DIR, 'docs/')
SPAM_FILE = os.path.join(TEST_DIR, 'spam.txt')
EGG_FILE = os.path.join(TEST_DIR, 'egg.txt')
ARGH_FILE = os.path.join(TEST_DIR, 'argh.md')
RESEARCH_FILE = os.path.join(DOC_DIR, 'research.txt')

def reset_dirs(directories):
    for directory in directories:
        if os.path.exists(directory):
            shutil.rmtree(directory)
        os.makedirs(directory)

def build_testing_entries():
    os.makedirs(HELLO_DIR)
    os.makedirs(DOC_DIR)
    with open(SPAM_FILE, "w") as text_file:
        text_file.write('spam file content')
    with open(EGG_FILE, "w") as text_file:
        text_file.write('egg file content')
    with open(ARGH_FILE, "w") as text_file:
        text_file.write('argh file content')
    with open(RESEARCH_FILE, "w") as text_file:
        text_file.write('research file content')


class TestOperations(unittest.TestCase): #pylint: disable=too-many-public-methods
    def setUp(self):
        reset_dirs([ROAMER_DATA_PATH, TRASH_DIR, TEST_DIR])
        build_testing_entries()
        self.session = MockSession(TEST_DIR)

    def test_directory_text_output(self):
        self.assertTrue('hello/' in self.session.text)
        self.assertTrue('docs/' in self.session.text)
        self.assertTrue('spam.txt' in self.session.text)
        self.assertTrue('egg.txt' in self.session.text)
        self.assertTrue('argh.md' in self.session.text)

    def test_create_new_file(self):
        self.session.add_entry('new_file.txt')
        self.session.process()
        path = os.path.join(TEST_DIR, 'new_file.txt')
        self.assertTrue(os.path.exists(path))
        self.assertTrue(os.path.isfile(path))

    def test_create_new_directory(self):
        self.session.add_entry('new_dir/')
        self.session.process()
        path = os.path.join(TEST_DIR, 'new_dir/')
        self.assertTrue(os.path.exists(path))
        self.assertTrue(os.path.isdir(path))

    def test_delete_file(self):
        self.session.remove_entry('argh.md')
        self.session.process()
        self.assertFalse(os.path.exists(ARGH_FILE))

    def test_delete_directory(self):
        self.session.remove_entry('hello/')
        self.session.process()
        self.assertFalse(os.path.exists(HELLO_DIR))

    def test_copy_file(self):
        digest = self.session.get_digest('egg.txt')
        self.session.add_entry('egg2.txt', digest)
        self.session.process()
        path = os.path.join(TEST_DIR, 'egg2.txt')
        self.assertTrue(os.path.exists(path))
        with open(path, 'r') as egg2_file:
            self.assertEqual(egg2_file.read(), 'egg file content')

    def test_copy_directory(self):
        digest = self.session.get_digest('docs/')
        self.session.add_entry('docs2/', digest)
        self.session.process()
        path = os.path.join(TEST_DIR, 'docs2/')
        self.assertTrue(os.path.exists(path))
        contents = os.listdir(path)
        self.assertEqual(contents, ['research.txt'])

    def test_rename_file(self):
        self.session.rename('argh.md', 'blarg.md')
        self.session.process()
        self.assertFalse(os.path.exists(ARGH_FILE))
        path = os.path.join(TEST_DIR, 'blarg.md')
        self.assertTrue(os.path.exists(path))
        with open(path, 'r') as blarg_file:
            self.assertEqual(blarg_file.read(), 'argh file content')

    def test_rename_directory(self):
        self.session.rename('docs/', 'my-docs/')
        self.session.process()
        self.assertFalse(os.path.exists(DOC_DIR))
        path = os.path.join(TEST_DIR, 'my-docs/')
        self.assertTrue(os.path.exists(path))
        contents = os.listdir(path)
        self.assertEqual(contents, ['research.txt'])

    def test_comment_line(self):
        original_entry_count = len([entry for entry in os.listdir(TEST_DIR)])
        self.session.add_entry('" Comment Line')
        self.session.process()
        entry_count = len([entry for entry in os.listdir(TEST_DIR)])
        self.assertEqual(entry_count, original_entry_count)

    def test_rename_file_to_directory(self):
        self.session.rename('hello/', 'hello.txt')
        with self.assertRaises(ValueError):
            self.session.process()

    def test_empty_lines(self):
        self.session.text += '\n    \n    \n '
        self.session.process()
        self.assertTrue(os.path.exists(ARGH_FILE))

    def test_multiple_simple_operations(self):
        self.test_create_new_file()<commit_msg>Add future test for clearing file contents<commit_after>"""
Integration tests for roamer
"""

import os
from os.path import expanduser
import shutil
import unittest
import time
from tests.mock_session import MockSession
BASE_ROAMER_PATH = os.environ.get('ROAMER_DATA_PATH') or expanduser('~/.roamer-data/')
os.environ["ROAMER_DATA_PATH"] = expanduser(os.path.join(BASE_ROAMER_PATH, 'tmp/test/.roamer-data'))
from roamer.constant import TEST_DIR, ROAMER_DATA_PATH, TRASH_DIR # pylint: disable=wrong-import-position

HELLO_DIR = os.path.join(TEST_DIR, 'hello/')
DOC_DIR = os.path.join(TEST_DIR, 'docs/')
SPAM_FILE = os.path.join(TEST_DIR, 'spam.txt')
EGG_FILE = os.path.join(TEST_DIR, 'egg.txt')
ARGH_FILE = os.path.join(TEST_DIR, 'argh.md')
RESEARCH_FILE = os.path.join(DOC_DIR, 'research.txt')

def reset_dirs(directories):
    for directory in directories:
        if os.path.exists(directory):
            shutil.rmtree(directory)
        os.makedirs(directory)

def build_testing_entries():
    os.makedirs(HELLO_DIR)
    os.makedirs(DOC_DIR)
    with open(SPAM_FILE, "w") as text_file:
        text_file.write('spam file content')
    with open(EGG_FILE, "w") as text_file:
        text_file.write('egg file content')
    with open(ARGH_FILE, "w") as text_file:
        text_file.write('argh file content')
    with open(RESEARCH_FILE, "w") as text_file:
        text_file.write('research file content')


class TestOperations(unittest.TestCase): #pylint: disable=too-many-public-methods
    def setUp(self):
        reset_dirs([ROAMER_DATA_PATH, TRASH_DIR, TEST_DIR])
        build_testing_entries()
        self.session = MockSession(TEST_DIR)

    def test_directory_text_output(self):
        self.assertTrue('hello/' in self.session.text)
        self.assertTrue('docs/' in self.session.text)
        self.assertTrue('spam.txt' in self.session.text)
        self.assertTrue('egg.txt' in self.session.text)
        self.assertTrue('argh.md' in self.session.text)

    def test_create_new_file(self):
        self.session.add_entry('new_file.txt')
        self.session.process()
        path = os.path.join(TEST_DIR, 'new_file.txt')
        self.assertTrue(os.path.exists(path))
        self.assertTrue(os.path.isfile(path))

    def test_create_new_directory(self):
        self.session.add_entry('new_dir/')
        self.session.process()
        path = os.path.join(TEST_DIR, 'new_dir/')
        self.assertTrue(os.path.exists(path))
        self.assertTrue(os.path.isdir(path))

    def test_delete_file(self):
        self.session.remove_entry('argh.md')
        self.session.process()
        self.assertFalse(os.path.exists(ARGH_FILE))

    def test_delete_directory(self):
        self.session.remove_entry('hello/')
        self.session.process()
        self.assertFalse(os.path.exists(HELLO_DIR))

    def test_copy_file(self):
        digest = self.session.get_digest('egg.txt')
        self.session.add_entry('egg2.txt', digest)
        self.session.process()
        path = os.path.join(TEST_DIR, 'egg2.txt')
        self.assertTrue(os.path.exists(path))
        with open(path, 'r') as egg2_file:
            self.assertEqual(egg2_file.read(), 'egg file content')

    def test_copy_directory(self):
        digest = self.session.get_digest('docs/')
        self.session.add_entry('docs2/', digest)
        self.session.process()
        path = os.path.join(TEST_DIR, 'docs2/')
        self.assertTrue(os.path.exists(path))
        contents = os.listdir(path)
        self.assertEqual(contents, ['research.txt'])

    def test_rename_file(self):
        self.session.rename('argh.md', 'blarg.md')
        self.session.process()
        self.assertFalse(os.path.exists(ARGH_FILE))
        path = os.path.join(TEST_DIR, 'blarg.md')
        self.assertTrue(os.path.exists(path))
        with open(path, 'r') as blarg_file:
            self.assertEqual(blarg_file.read(), 'argh file content')

    def test_rename_directory(self):
        self.session.rename('docs/', 'my-docs/')
        self.session.process()
        self.assertFalse(os.path.exists(DOC_DIR))
        path = os.path.join(TEST_DIR, 'my-docs/')
        self.assertTrue(os.path.exists(path))
        contents = os.listdir(path)
        self.assertEqual(contents, ['research.txt'])

    def disabled_clear_persisted_file_contents(self):
        self.session.remove_entry('argh.md')
        self.session.add_entry('argh.md')
        self.session.process()
        path = ARGH_FILE
        self.assertFalse(os.path.exists(path))
        with open(path, 'r') as argh_file:
            self.assertEqual(argh_file.read(), 'argh file content')

    def test_comment_line(self):
        original_entry_count = len([entry for entry in os.listdir(TEST_DIR)])
        self.session.add_entry('" Comment Line')
        self.session.process()
        entry_count = len([entry for entry in os.listdir(TEST_DIR)])
        self.assertEqual(entry_count, original_entry_count)

    def test_rename_file_to_directory(self):
        self.session.rename('hello/', 'hello.txt')
        with self.assertRaises(ValueError):
            self.session.process()

    def test_empty_lines(self):
        self.session.text += '\n    \n    \n '
        self.session.process()
        self.assertTrue(os.path.exists(ARGH_FILE))

    def test_multiple_simple_operations(self):
        self.test_create_new_file()
{'fraction_non_alphanumeric': 0.13250797949511559, 'fraction_numerical': 0.001160653834993713, 'mean_word_length': 3.1643173580346353, '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': '2101694', 'n_tokens_mistral': 3453, 'n_tokens_neox': 3428, 'n_words': 548}