"""An upload-only rewrite must preserve commands that also produce other build inputs."""
import copy
import unittest

import prepare_crashlytics_build as build


class PhaseEffectTests(unittest.TestCase):
    def test_redirected_builtins_are_build_work_even_without_another_executable(self):
        for command in ('echo public > Generated.swift', 'echo enabled >> build.env',
                        ': > generated.config', 'BUILD=release > generated.config'):
            with self.subTest(command=command):
                script = command + '\nupload-symbols -gsp app.plist -p ios symbols'
                self.assertFalse(build.dedicated(script))
                document = {'objects': {'phase': {'isa': 'PBXShellScriptBuildPhase',
                            'name': 'Crashlytics and generated input', 'shellScript': script}}}
                original = copy.deepcopy(document)
                with self.assertRaisesRegex(ValueError, 'shares the build phase'):
                    build.rewrite(document)
                self.assertEqual(document, original)

    def test_supported_generated_guards_remain_upload_only(self):
        self.assertTrue(build.dedicated(
            'if [ -f "$PODS_ROOT/FirebaseCrashlytics/upload-symbols" ]; then\n'
            '"$PODS_ROOT/FirebaseCrashlytics/upload-symbols" -gsp app.plist -p ios symbols\n'
            'else echo "warning: upload-symbols absent"; fi'))


if __name__ == '__main__':
    unittest.main()
