# -*- python -*-
# test_host_specific_configuration.py - Unit tests for
# swift_build_support.host_specific_configuration
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

import unittest
from argparse import Namespace

from swift_build_support.host_specific_configuration import \
    HostSpecificConfiguration


class ToolchainTestCase(unittest.TestCase):

    def test_should_configure_and_build_when_deployment_is_all(self):
        host_target = 'macosx-x86_64'
        args = self.default_args()
        args.build_osx = True
        args.build_ios_device = True
        args.host_target = host_target
        args.stdlib_deployment_targets = [host_target, 'iphoneos-arm64']
        args.build_stdlib_deployment_targets = 'all'

        hsc = HostSpecificConfiguration(host_target, args)

        self.assertEqual(len(hsc.sdks_to_configure), 2)
        self.assertIn('OSX', hsc.sdks_to_configure)
        self.assertIn('IOS', hsc.sdks_to_configure)

        self.assertEqual(len(hsc.swift_stdlib_build_targets), 2)
        self.assertIn('swift-test-stdlib-macosx-x86_64',
                      hsc.swift_stdlib_build_targets)
        self.assertIn('swift-test-stdlib-iphoneos-arm64',
                      hsc.swift_stdlib_build_targets)

    def test_should_only_deployment_if_specified(self):
        host_target = 'macosx-x86_64'
        args = self.default_args()
        args.build_osx = True
        args.build_ios_device = True
        args.host_target = host_target
        args.stdlib_deployment_targets = [host_target, 'iphoneos-arm64']
        args.build_stdlib_deployment_targets = ['iphoneos-arm64']

        hsc = HostSpecificConfiguration(host_target, args)

        self.assertEqual(len(hsc.sdks_to_configure), 2)
        self.assertIn('OSX', hsc.sdks_to_configure)
        self.assertIn('IOS', hsc.sdks_to_configure)

        self.assertEqual(len(hsc.swift_stdlib_build_targets), 1)
        self.assertIn('swift-test-stdlib-iphoneos-arm64',
                      hsc.swift_stdlib_build_targets)

    def test_should_configure_and_build_when_cross_compiling(self):
        args = self.default_args()
        args.build_ios_device = True
        args.host_target = 'macosx-x86_64'

        hsc = HostSpecificConfiguration('iphoneos-arm64', args)

        self.assertEqual(len(hsc.sdks_to_configure), 1)
        self.assertIn('IOS', hsc.sdks_to_configure)

        self.assertEqual(len(hsc.swift_stdlib_build_targets), 1)
        self.assertIn('swift-test-stdlib-iphoneos-arm64',
                      hsc.swift_stdlib_build_targets)

    def generate_should_skip_building_platform(
            host_target, sdk_name, build_target, build_arg_name):
        def test(self):
            args = self.default_args()
            args.host_target = host_target
            args.stdlib_deployment_targets = [host_target]
            args.build_stdlib_deployment_targets = 'all'

            before = HostSpecificConfiguration(host_target, args)
            self.assertIn(sdk_name, before.sdks_to_configure)
            self.assertNotIn(build_target, before.swift_stdlib_build_targets)

            setattr(args, build_arg_name, True)
            after = HostSpecificConfiguration(host_target, args)
            self.assertIn(sdk_name, after.sdks_to_configure)
            self.assertIn(build_target, after.swift_stdlib_build_targets)
        return test

    test_should_skip_building_android =\
        generate_should_skip_building_platform(
            'android-armv7',
            'ANDROID',
            'swift-test-stdlib-android-armv7',
            'build_android')
    test_should_skip_building_cygwin =\
        generate_should_skip_building_platform(
            'cygwin-x86_64',
            'CYGWIN',
            'swift-test-stdlib-cygwin-x86_64',
            'build_cygwin')
    test_should_skip_building_freebsd =\
        generate_should_skip_building_platform(
            'freebsd-x86_64',
            'FREEBSD',
            'swift-test-stdlib-freebsd-x86_64',
            'build_freebsd')
    test_should_skip_building_ios =\
        generate_should_skip_building_platform(
            'iphoneos-arm64',
            'IOS',
            'swift-test-stdlib-iphoneos-arm64',
            'build_ios_device')
    test_should_skip_building_ios_sim =\
        generate_should_skip_building_platform(
            'iphonesimulator-x86_64',
            'IOS_SIMULATOR',
            'swift-test-stdlib-iphonesimulator-x86_64',
            'build_ios_simulator')
    test_should_skip_building_linux =\
        generate_should_skip_building_platform(
            'linux-x86_64',
            'LINUX',
            'swift-test-stdlib-linux-x86_64',
            'build_linux')
    test_should_skip_building_osx =\
        generate_should_skip_building_platform(
            'macosx-x86_64',
            'OSX',
            'swift-test-stdlib-macosx-x86_64',
            'build_osx')
    test_should_skip_building_tvos =\
        generate_should_skip_building_platform(
            'appletvos-arm64',
            'TVOS',
            'swift-test-stdlib-appletvos-arm64',
            'build_tvos_device')
    test_should_skip_building_tvos_sim =\
        generate_should_skip_building_platform(
            'appletvsimulator-x86_64', 'TVOS_SIMULATOR',
            'swift-test-stdlib-appletvsimulator-x86_64',
            'build_tvos_simulator')
    test_should_skip_building_watchos =\
        generate_should_skip_buildi
ng_platform(
            'watchos-armv7k',
            'WATCHOS',
            'swift-test-stdlib-watchos-armv7k',
            'bui
ld_watchos_device')
    test_should_skip_building_watchos_sim =\
        generate_should_skip_building_platform(
            'watchsimulator-i386',
            'WATCHOS_SIMULATOR',
            'swift-test-stdlib-watchsimulator-i386',
            'build_watchos_simulator')

    def generate_should_build_full_targets_when_test(test_arg_name):
        def test(self):
            host_target = 'macosx-x86_64'
            args = self.default_args()
            args.build_osx = True
            args.host_target = host_target
            args.stdlib_deployment_targets = [host_target]
            args.build_stdlib_deployment_targets = 'all'

            before = HostSpecificConfiguration(host_target, args)
            self.assertIn('swift-test-stdlib-macosx-x86_64',
                          before.swift_stdlib_build_targets)
            self.assertNotIn('swift-stdlib-macosx-x86_64',
                             before.swift_stdlib_build_targets)

            setattr(args, test_arg_name, True)
            after = HostSpecificConfiguration(host_target, args)
            self.assertIn('swift-stdlib-macosx-x86_64',
                          after.swift_stdlib_build_targets)
            self.assertNotIn('swift-test-stdlib-macosx-x86_64',
                             after.swift_stdlib_build_targets)
        return test

    test_should_build_full_targets_when_unittest_extra =\
        generate_should_build_full_targets_when_test(
            'build_swift_stdlib_unittest_extra')
    test_should_build_full_targets_when_validation_test =\
        generate_should_build_full_targets_when_test(
            'validation_test')
    test_should_build_full_targets_when_long_test =\
        generate_should_build_full_targets_when_test(
            'long_test')
    test_should_build_full_targets_when_stress_test =\
        generate_should_build_full_targets_when_test(
            'stress_test')

    def generate_should_skip_testing_platform(
            host_target, build_arg_name, test_arg_name):
        def test(self):
            args = self.default_args()
            setattr(args, build_arg_name, True)
            args.host_target = host_target
            args.stdlib_deployment_targets = [host_target]
            args.build_stdlib_deployment_targets = 'all'

            before = HostSpecificConfiguration