<commit_before>#  Copyright 2008 Nokia Siemens Networks Oyj
#  
#  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.

import unittest
import tempfile
import os

from robotide.model.tcuk import TestCase, UserKeyword
from robotide.model.files import TestSuiteFactory, TestCaseFile, ResourceFileFactory,\
    _EmptyTestSuite
from resources import MockSerializer, FakeSuite, FakeTestCase, FakeUserKeyword, \
    FakeResource, COMPLEX_SUITE_PATH
from robotide.errors import NoRideError

from robot.utils.asserts import assert_false, assert_true, assert_equals,\
    assert_raises_with_msg, assert_not_equals


DIRPATH = os.path.join(tempfile.gettempdir(), 'ride_model_utest_dir')
if not os.path.exists(DIRPATH):
    os.mkdir(DIRPATH)
FILEPATH = os.path.join(DIRPATH, 'tests.tsv')
RESOURCEPATH = os.path.join(DIRPATH, 'resource.tsv')
INITPATH = os.path.join(DIRPATH, '__init__.tsv') 


class TestNoRide(unittest.TestCase):

    def test_creating_suite_with_no_ride_meta_fails(self):
        msg = "Test data file 'fake/suite.html' is not supposed to be edited with RIDE."
        assert_raises_with_msg(NoRideError, msg, _NoRideFakeSuite)
        

class TestModifiedOnDiskWithFileSuite(unittest.TestCase):

    def setUp(self):
        open(FILEPATH, 'w').write('*Test Cases*\nRide Unit Test\tNo Operation\n')

    def test_mtime(self):
        suite = FakeSuite(path=FILEPATH)
        assert_false(suite.has_been_modified_on_disk())
        os.utime(FILEPATH, (1,1))
        assert_true(suite.has_been_modified_on_disk())

    def test_size_change(self):
        os.utime(FILEPATH, None)
        suite = FakeSuite(path=FILEPATH)
        open(FILEPATH, 'a').write('#Ninja edit\n')
        assert_true(suite.has_been_modified_on_disk())

    def test_reload(self):
        suite = TestSuiteFactory(FILEPATH)
        assert_equals(len(suite.tests), 1)
        open(FILEPATH, 'a').write('Second Test\tLog\tHello World!\n')
        suite.reload_from_disk()
        assert_equals(len(suite.tests), 2)
        assert_equals(suite.tests[-1].name, 'Second Test')

    def test_overwrite(self):
        suite = TestSuiteFactory(FILEPATH)
        os.utime(FILEPATH, (1,1))
        assert_true(suite.has_been_modified_on_disk())
        suite.serialize(force=True)
        assert_false(suite.has_been_modified_on_disk())


class TestModifiedOnDiskWithDirectorySuite(unittest.TestCase):

    def setUp(self):
        open(INITPATH, 'w').write('*Settings*\nDocumentation\tRide unit testing file\n')

    def test_reload_with_directory_suite(self):
        suite = TestSuiteFactory(DIRPATH)
        orig_doc = suite.settings.doc.get_str_value()
        open(INITPATH, 'a').write('...\tAdded more documentation')
        suite.reload_from_disk()
        s = suite.settings
        assert_not_equals(orig_doc, s.doc.get_str_value())
        assert_true(s.doc.get_str_value().endswith('Added more documentation'))

    def test_mtime_with_directory_suite(self):
        suite = FakeSuite(path=INITPATH)
        assert_false(suite.has_been_modified_on_disk())
        os.utime(INITPATH, (1,1))
        assert_true(suite.has_been_modified_on_disk())


class TestModifiedOnDiskWithresource(unittest.TestCase):

    def setUp(self):
        open(RESOURCEPATH, 'w').write('*Keywords*\nUnit Test Keyword\tNo Operation\n')

    def test_reload_with_resource(self):
        resource = ResourceFileFactory(RESOURCEPATH)
        assert_equals(len(resource.keywords), 1)
        open(RESOURCEPATH, 'a').write('Ninjaed Keyword\tLog\tI am taking over!\n')<commit_msg>Tests need to created too, otherwise test execution order matters<commit_after>#  Copyright 2008 Nokia Siemens Networks Oyj
#  
#  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.

import unittest
import tempfile
import os

from robotide.model.tcuk import TestCase, UserKeyword
from robotide.model.files import TestSuiteFactory, TestCaseFile, ResourceFileFactory,\
    _EmptyTestSuite
from resources import MockSerializer, FakeSuite, FakeTestCase, FakeUserKeyword, \
    FakeResource, COMPLEX_SUITE_PATH
from robotide.errors import NoRideError

from robot.utils.asserts import assert_false, assert_true, assert_equals,\
    assert_raises_with_msg, assert_not_equals


DIRPATH = os.path.join(tempfile.gettempdir(), 'ride_model_utest_dir')
if not os.path.exists(DIRPATH):
    os.mkdir(DIRPATH)
FILEPATH = os.path.join(DIRPATH, 'tests.tsv')
RESOURCEPATH = os.path.join(DIRPATH, 'resource.tsv')
INITPATH = os.path.join(DIRPATH, '__init__.tsv') 


class TestNoRide(unittest.TestCase):

    def test_creating_suite_with_no_ride_meta_fails(self):
        msg = "Test data file 'fake/suite.html' is not supposed to be edited with RIDE."
        assert_raises_with_msg(NoRideError, msg, _NoRideFakeSuite)


class TestModifiedOnDiskWithFileSuite(unittest.TestCase):

    def setUp(self):
        open(FILEPATH, 'w').write('*Test Cases*\nRide Unit Test\tNo Operation\n')

    def test_mtime(self):
        suite = FakeSuite(path=FILEPATH)
        assert_false(suite.has_been_modified_on_disk())
        os.utime(FILEPATH, (1,1))
        assert_true(suite.has_been_modified_on_disk())

    def test_size_change(self):
        os.utime(FILEPATH, None)
        suite = FakeSuite(path=FILEPATH)
        open(FILEPATH, 'a').write('#Ninja edit\n')
        assert_true(suite.has_been_modified_on_disk())

    def test_reload(self):
        suite = TestSuiteFactory(FILEPATH)
        assert_equals(len(suite.tests), 1)
        open(FILEPATH, 'a').write('Second Test\tLog\tHello World!\n')
        suite.reload_from_disk()
        assert_equals(len(suite.tests), 2)
        assert_equals(suite.tests[-1].name, 'Second Test')

    def test_overwrite(self):
        suite = TestSuiteFactory(FILEPATH)
        os.utime(FILEPATH, (1,1))
        assert_true(suite.has_been_modified_on_disk())
        suite.serialize(force=True)
        assert_false(suite.has_been_modified_on_disk())


class TestModifiedOnDiskWithDirectorySuite(unittest.TestCase):

    def setUp(self):
        open(FILEPATH, 'w').write('*Test Cases*\nRide Unit Test\tNo Operation\n')
        open(INITPATH, 'w').write('*Settings*\nDocumentation\tRide unit testing file\n')

    def test_reload_with_directory_suite(self):
        suite = TestSuiteFactory(DIRPATH)
        orig_doc = suite.settings.doc.get_str_value()
        open(INITPATH, 'a').write('...\tAdded more documentation')
        suite.reload_from_disk()
        s = suite.settings
        assert_not_equals(orig_doc, s.doc.get_str_value())
        assert_true(s.doc.get_str_value().endswith('Added more documentation'))

    def test_mtime_with_directory_suite(self):
        suite = FakeSuite(path=INITPATH)
        assert_false(suite.has_been_modified_on_disk())
        os.utime(INITPATH, (1,1))
        assert_true(suite.has_been_modified_on_disk())


class TestModifiedOnDiskWithresource(unittest.TestCase):

    def setUp(self):
        open(RESOURCEPATH, 'w').write('*Keywords*\nUnit Test Keyword\tNo Operation\n')

    def test_reload_with_resource(self):
        resource = ResourceFileFactory(RESOURCEPATH)
        assert_equals(len(resource.keywords), 1)
        open(RESOURCEPATH, 'a').write('Ninjaed Keyword\tLog\tI am taking over!\n')
{'fraction_non_alphanumeric': 0.10368382532324957, 'fraction_numerical': 0.004391314954867041, 'mean_word_length': 3.9872262773722627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1175196', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2550, 'n_words': 552}