<commit_before># Copyright 2012-2013 Isotoma Limited
#
# 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 __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import yaml
from twisted.trial import unittest

from buildbot.plugins import steps, util
from buildbot_travis.travisyml import TravisYml, TravisYmlInvalid


class TravisYmlTestCase(unittest.TestCase):

    def setUp(self):
        self.t = TravisYml()
        self.t.config = {}


class TestYamlParsing(TravisYmlTestCase):

    def test_basic(self):
        self.t.parse("""
        language: python
        """)

    def test_with_scripts(self):
        self.t.parse("""
        language: python
        script:
            - echo ok
        """)
        self.failUnlessEqual(self.t.script, ["echo ok"])

    def test_with_interpolated_scripts(self):
        self.t.parse("""
        language: python
        script:
            - !i echo ok
        """)
        self.failUnlessEqual(self.t.script, [util.Interpolate("echo ok")])

    def test_with_interpolated_scripts_in_list(self):
        self.t.parse("""
        language: python
        script:
          - title: make dist
            cmd: [ "make", !Interpolate "REVISION=%(prop:got_revision:-%(src::revision:-unknown)s)s", "dist" ]
        """)
        self.failUnlessEqual(self.t.script, [{'cmd': [
            'make',
            util.Interpolate(u'REVISION=%(prop:got_revision:-%(src::revision:-unknown)s)s'),
            'dist'],
            'title': 'make dist'}])

    def test_with_plugin_step(self):
        if not hasattr(steps.CMake, "compare_attrs"):
            return unittest.SkipTest("Test needs buildbot buildstep comparison")
        self.t.parse("""
        language: python
        script:
            - !CMake target
        """)
        self.failUnlessEqual(self.t.script, [steps.CMake("target")])

    def test_with_plugin_step_with_parse_error(self):
        self.assertRaises(TravisYmlInvalid, self.t.parse, """
        language: python
        script:
            - !CMake [ tar:get ]
        """)

    def test_yaml_not_polluted(self):
        """yaml.load should not recognise Interpolate contruct"""
        self.assertRaises(yaml.constructor.ConstructorError, yaml.safe_load, """
            - !i foo
            """)

class TestEnv(TravisYmlTestCase):

    def test_noenv(self):
        self.t.parse_envs()
        self.assertEqual(self.t.environments, [{}])

    def test_singleenv(self):
        self.t.config["env"] = "FOO=1 BAR=2"
        self.t.parse_envs()
        self.assertEqual(self.t.environments, [dict(FOO='1', BAR='2')])

        self.t.parse_matrix()
        self.assertEqual(
            self.t.matrix, [dict(python="python2.6", env=dict(FOO='1', BAR='2')), ])

    def test_multienv(self):
        self.t.config["env"] = ["FOO=1 BAR=2", "FOO=2 BAR=1"]
        self.t.parse_envs()
        self.assertEqual(
            self.t.environments, [dict(FOO='1', BAR='2'), dict(FOO='2', BAR='1')])

        self.t.parse_matrix()<commit_msg>fix test which used a parse error that is no more a parse error<commit_after># Copyright 2012-2013 Isotoma Limited
#
# 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 __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import yaml
from twisted.trial import unittest

from buildbot.plugins import steps, util
from buildbot_travis.travisyml import TravisYml, TravisYmlInvalid


class TravisYmlTestCase(unittest.TestCase):

    def setUp(self):
        self.t = TravisYml()
        self.t.config = {}


class TestYamlParsing(TravisYmlTestCase):

    def test_basic(self):
        self.t.parse("""
        language: python
        """)

    def test_with_scripts(self):
        self.t.parse("""
        language: python
        script:
            - echo ok
        """)
        self.failUnlessEqual(self.t.script, ["echo ok"])

    def test_with_interpolated_scripts(self):
        self.t.parse("""
        language: python
        script:
            - !i echo ok
        """)
        self.failUnlessEqual(self.t.script, [util.Interpolate("echo ok")])

    def test_with_interpolated_scripts_in_list(self):
        self.t.parse("""
        language: python
        script:
          - title: make dist
            cmd: [ "make", !Interpolate "REVISION=%(prop:got_revision:-%(src::revision:-unknown)s)s", "dist" ]
        """)
        self.failUnlessEqual(self.t.script, [{'cmd': [
            'make',
            util.Interpolate(u'REVISION=%(prop:got_revision:-%(src::revision:-unknown)s)s'),
            'dist'],
            'title': 'make dist'}])

    def test_with_plugin_step(self):
        if not hasattr(steps.CMake, "compare_attrs"):
            return unittest.SkipTest("Test needs buildbot buildstep comparison")
        self.t.parse("""
        language: python
        script:
            - !CMake target
        """)
        self.failUnlessEqual(self.t.script, [steps.CMake("target")])

    def test_with_plugin_step_with_parse_error(self):
        self.assertRaises(TravisYmlInvalid, self.t.parse, """
        language: python
        script:
            - !CMake [ "target ]
        """)

    def test_yaml_not_polluted(self):
        """yaml.load should not recognise Interpolate contruct"""
        self.assertRaises(yaml.constructor.ConstructorError, yaml.safe_load, """
            - !i foo
            """)

class TestEnv(TravisYmlTestCase):

    def test_noenv(self):
        self.t.parse_envs()
        self.assertEqual(self.t.environments, [{}])

    def test_singleenv(self):
        self.t.config["env"] = "FOO=1 BAR=2"
        self.t.parse_envs()
        self.assertEqual(self.t.environments, [dict(FOO='1', BAR='2')])

        self.t.parse_matrix()
        self.assertEqual(
            self.t.matrix, [dict(python="python2.6", env=dict(FOO='1', BAR='2')), ])

    def test_multienv(self):
        self.t.config["env"] = ["FOO=1 BAR=2", "FOO=2 BAR=1"]
        self.t.parse_envs()
        self.assertEqual(
            self.t.environments, [dict(FOO='1', BAR='2'), dict(FOO='2', BAR='1')])

        self.t.parse_matrix()
{'fraction_non_alphanumeric': 0.12769532343881265, 'fraction_numerical': 0.00784094091290955, 'mean_word_length': 2.670606372045221, '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': '5987197', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2176, 'n_words': 506}