# Copyright (C) 2008-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for finding, parsing and searching rule-based preferences."""

import sys

from bzrlib import (
    errors,
    rules,
    tests,
    )


class TestIniBasedRulesSearcher(tests.TestCase):

    def make_searcher(self, text):
        """Make a _RulesSearcher from a string"""
        if text is None:
            lines = None
        else:
            lines = text.splitlines()
        return rules._IniBasedRulesSearcher(lines)

    def test_unknown_namespace(self):
        self.assertRaises(errors.UnknownRules, rules._IniBasedRulesSearcher,
            ["[junk]", "foo=bar"])

    def test_get_items_file_missing(self):
        rs = self.make_searcher(None)
        self.assertEquals((), rs.get_items('a.txt'))
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
        self.assertEquals(None, rs.get_single_value('a.txt', 'foo'))

    def test_get_items_file_empty(self):
        rs = self.make_searcher("")
        self.assertEquals((), rs.get_items('a.txt'))
        self.assertEquals((), rs.get_selected_items('a.txt', ['foo']))
        self.assertEquals(None, rs.get_single_value('a.txt', 'foo'))

    def test_get_items_from_extension_match(self):
        rs = self.make_searcher("[name *.txt]\nfoo=bar\na=True\n")
        self.assertEquals((), rs.get_items('a.py'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('a.txt'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('dir/a.txt'))
        self.assertEquals((('foo', 'bar'),),
            rs.get_selected_items('a.txt', ['foo']))
        self.assertEquals('bar', rs.get_single_value('a.txt', 'foo'))

    def test_get_items_from_multiple_glob_match(self):
        rs = self.make_searcher(
            "[name *.txt *.py 'x x' \"y y\"]\nfoo=bar\na=True\n")
        self.assertEquals((), rs.get_items('NEWS'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('a.py'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('a.txt'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('x x'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('y y'))
        self.assertEquals('bar', rs.get_single_value('a.txt', 'foo'))

    def test_get_items_pathname_match(self):
        rs = self.make_searcher("[name ./a.txt]\nfoo=baz\n")
        self.assertEquals((('foo', 'baz'),),
            rs.get_items('a.txt'))
        self.assertEquals('baz', rs.get_single_value('a.txt', 'foo'))
        self.assertEquals((), rs.get_items('dir/a.txt'))
        self.assertEquals(None, rs.get_single_value('dir/a.txt', 'foo'))

    def test_get_items_match_first(self):
        rs = self.make_searcher(
            "[name ./a.txt]\nfoo=baz\n"
            "[name *.txt]\nfoo=bar\na=True\n")
        self.assertEquals((('foo', 'baz'),),
            rs.get_items('a.txt'))
        self.assertEquals('baz', rs.get_single_value('a.txt', 'foo'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('dir/a.txt'))
        self.assertEquals('bar', rs.get_single_value('dir/a.txt', 'foo'))


class TestStackedRulesSearcher(tests.TestCase):

    def make_searcher(self, text1=None, text2=None):
        """Make a _StackedRulesSearcher with 0, 1 or 2 items"""
        searchers = []
        if text1 is not None:
            searchers.append(rules._IniBasedRulesSearcher(
                text1.splitlines()))
        if text2 is not None:
            searchers.append(rules._IniBasedRulesSearcher(
                text2.splitlines()))
        return rules._StackedRulesSearcher(searchers)

    def test_stack_searching(self):
        rs = self.make_searcher(
            "[name ./a.txt]\nfoo=baz\n",
            "[name *.txt]\nfoo=bar\na=True\n")
        self.assertEquals((('foo', 'baz'),),
            rs.get_items('a.txt'))
        self.assertEquals('baz', rs.get_single_value('a.txt', 'foo'))
        self.assertEquals(None, rs.get_single_value('a.txt', 'a'))
        self.assertEquals((('foo', 'bar'), ('a', 'True')),
            rs.get_items('dir/a.txt'))
        self.assertEquals('bar', rs.get_single_value('dir/a.txt', 'foo'))
        self.assertEquals('True', rs.get_single_value('dir/a.txt', 'a'))


class TestRulesPath(tests.TestCase):

    def setUp(self):
        super(TestRulesPath, self).setUp()
        self.overrideEnv('HOME', '/home/bogus')
        if sys.platform == 'win32':
            self.overrideEnv(
                'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
            self.bzr_home = \
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
        else:
            self.bzr_home = '/home/bogus/.bazaar'

    def test_rules_filename(self):
        self.assertEqual(rules.rules_filename(),
                         self.bzr_home + '/rules')
