"""Integration tests for germinate."""

# Copyright (C) 2011, 2012 Canonical Ltd.
#
# Germinate 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, or (at your option) any
# later version.
#
# Germinate 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 Germinate; see the file COPYING.  If not, see
# <https://www.gnu.org/licenses/>.

import json

from germinate.scripts import germinate_main
from germinate.tests.helpers import TestCase


class TestGerminate(TestCase):
    def runGerminate(self, *args):
        self.useTempDir()
        argv = ["germinate"]
        argv.extend(["-S", "file://%s" % self.seeds_dir])
        argv.extend(["-m", "file://%s" % self.archive_dir])
        argv.extend(args)
        self.assertEqual(0, germinate_main.main(argv))

    def parseOutput(self, output_name):
        output_dict = {}
        with open(output_name) as output:
            output.readline()
            output.readline()
            for line in output:
                if line.startswith("-"):
                    break
                fields = [field.strip() for field in line.split("|")]
                output_dict[fields[0]] = fields[1:]
        return output_dict

    def test_trivial(self):
        self.addSource(
            "warty", "main", "hello", "1.0-1", ["hello", "hello-dependency"]
        )
        self.addPackage(
            "warty",
            "main",
            "i386",
            "hello",
            "1.0-1",
            fields={"Depends": "hello-dependency"},
        )
        self.addPackage(
            "warty",
            "main",
            "i386",
            "hello-dependency",
            "1.0-1",
            fields={"Source": "hello"},
        )
        self.addSeed("ubuntu.warty", "supported")
        self.addSeedPackage("ubuntu.warty", "supported", "hello")
        self.runGerminate(
            "-s", "ubuntu.warty", "-d", "warty", "-a", "i386", "-c", "main"
        )

        supported = self.parseOutput("supported")
        self.assertTrue("hello" in supported)
        self.assertTrue("hello-dependency" in supported)
        with open("supported.json") as fp:
            supported_json = json.load(fp)
        self.assertEqual(set(supported_json), set(supported))

        all_ = self.parseOutput("all")
        self.assertTrue("hello" in all_)
        self.assertTrue("hello-dependency" in all_)

        with open("all.json") as fp:
            all_json = json.load(fp)
        self.assertEqual(set(all_json), set(all_))

    def test_snap(self):
        # Need Packages
        self.addSource(
            "warty", "main", "hello", "1.0-1", ["hello", "hello-dependency"]
        )
        self.addPackage(
            "warty",
            "main",
            "i386",
            "hello-dependency",
            "1.0-1",
            fields={"Source": "hello"},
        )
        self.addSeed("ubuntu.warty", "supported")
        self.addSeedSnap("ubuntu.warty", "supported", "mycoolsnap")
        self.runGerminate(
            "-s", "ubuntu.warty", "-d", "warty", "-a", "i386", "-c", "main"
        )

        supported = self.parseOutput("supported.snaps")
        self.assertIn("mycoolsnap", supported)

        all_ = self.parseOutput("all.snaps")
        self.assertIn("mycoolsnap", all_)

        debs = self.parseOutput("supported")
        self.assertNotIn("mycoolsnap", debs)

    def test_classic_snap(self):
        # Need Packages
        self.addSource(
            "warty", "main", "hello", "1.0-1", ["hello", "hello-dependency"]
        )
        self.addPackage(
            "warty",
            "main",
            "i386",
            "hello-dependency",
            "1.0-1",
            fields={"Source": "hello"},
        )
        self.addSeed("ubuntu.warty", "supported")
        self.addSeedSnap("ubuntu.warty", "supported", "mycoolsnap/classic")
        self.runGerminate(
            "-s", "ubuntu.warty", "-d", "warty", "-a", "i386", "-c", "main"
        )

        supported = self.parseOutput("supported.snaps")
        self.assertIn("mycoolsnap (classic)", supported)

        all_ = self.parseOutput("all.snaps")
        self.assertIn("mycoolsnap (classic)", all_)

        debs = self.parseOutput("supported")
        self.assertNotIn("mycoolsnap (classic)", debs)
