import os
import sys
import unittest

sys.path.append(os.path.abspath("."))
from diffios import DiffiosConfig
from utils import baseline_blocks, baseline_partition


class DiffiosConfigTest(unittest.TestCase):

    def setUp(self):
        self.configs_dir = os.path.abspath(os.path.join("tests", "configs"))
        self.config = os.path.join(self.configs_dir, "baseline.conf")
        self.dc = DiffiosConfig(self.config)

    def test_default_ignore_filename(self):
        expected = os.path.join(os.getcwd(), "diffios_ignore")
        actual = self.dc.ignore_filename
        self.assertEqual(expected, actual)

    def test_alternative_ignore_filename(self):
        alt_ignore_path = os.path.join(os.getcwd(), "tests", "alt_diffios_ignore")
        expected = alt_ignore_path
        dc = DiffiosConfig(self.config, ignores=alt_ignore_path)
        actual = dc.ignore_filename
        self.assertEqual(expected, actual)

    def test_ignore_filename_is_False_if_ignore_file_does_not_exist(self):
        dc = DiffiosConfig(self.config, ignores="alt_ignore_file")
        self.assertFalse(dc.ignore_filename)

    def test_ignore_filename_is_False_if_ignores_is_False(self):
        dc = DiffiosConfig(self.config, ignores=False)
        self.assertFalse(dc.ignore_filename)

    def test_ignore_filename_is_False_if_ignores_is_empty_list(self):
        dc = DiffiosConfig(self.config, ignores=[])
        self.assertFalse(dc.ignore_filename)

    def test_ignore_filename_is_False_if_ignores_is_empty_string(self):
        dc = DiffiosConfig(self.config, ignores="")
        self.assertFalse(dc.ignore_filename)

    def test_ignore_filename_is_False_if_ignores_is_zero(self):
        dc = DiffiosConfig(self.config, ignores=0)
        self.assertFalse(dc.ignore_filename)

    def test_config_filename(self):
        actual = self.config
        expected = self.dc.config_filename
        self.assertEqual(expected, actual)

    # config property now removes invalid lines
    # def test_config(self):
        # expected = [l.rstrip() for l in open(self.config).readlines()]
        # actual = self.df.config
        # self.assertEqual(expected, actual)

    def test_hostname(self):
        expected = "BASELINE01"
        actual = self.dc.hostname
        self.assertEqual(expected, actual)

    def test_blocks(self):
        expected = baseline_blocks()
        actual = self.dc.config_blocks
        self.assertEqual(expected, actual)

    def test_ignore_lines(self):
        ignore_file = open("diffios_ignore").readlines()
        expected = [l.strip().lower() for l in ignore_file]
        actual = self.dc.ignores
        self.assertEqual(expected, actual)

    def test_ignored(self):
        expected = baseline_partition().ignored
        actual = self.dc.ignored
        self.assertEqual(expected, actual)

    def test_recorded(self):
        expected = baseline_partition().recorded
        actual = self.dc.recorded
        self.assertEqual(expected, actual)

@@ -31,16 +31,53 @@
 ittest%0A%0A
+THIS_DIR = os.path.dirname(__file__)%0A
 sys.path
@@ -106,16 +106,17 @@
 h(%22.%22))%0A
+%0A
 from dif
@@ -739,28 +739,16 @@
 oin(
-os.getcwd(), %22tests%22
+THIS_DIR
 , %22a

add THIS_DIR