Beispiel #1
0
        private PythonPlatform CreatePlatform(
            string[] supportedVersions = null,
            string defaultVersion      = null,
            string detectedVersion     = null,
            BuildScriptGeneratorOptions commonOptions = null,
            PythonScriptGeneratorOptions pythonScriptGeneratorOptions = null)
        {
            supportedVersions = supportedVersions ?? new[] { defaultVersion };
            defaultVersion    = defaultVersion ?? Common.PythonVersions.Python37Version;
            var versionProvider = new TestPythonVersionProvider(
                supportedPythonVersions: supportedVersions,
                defaultVersion: defaultVersion);

            commonOptions = commonOptions ?? new BuildScriptGeneratorOptions();
            pythonScriptGeneratorOptions = pythonScriptGeneratorOptions ?? new PythonScriptGeneratorOptions();
            var detector = new TestPythonPlatformDetector(detectedVersion: detectedVersion);

            return(new PythonPlatform(
                       Options.Create(commonOptions),
                       Options.Create(pythonScriptGeneratorOptions),
                       versionProvider,
                       NullLogger <PythonPlatform> .Instance,
                       detector,
                       new PythonPlatformInstaller(Options.Create(commonOptions), NullLoggerFactory.Instance)));
        }
        public void Detect_ReturnsVersionFromOptions_EvenIfRuntimeTextFileHasVersion()
        {
            // Arrange
            var expectedVersion              = "1.2.3";
            var runtimeTextFileVersion       = "2.5.0";
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions()
            {
                PythonVersion = expectedVersion
            };
            var platform = CreatePlatform(
                supportedVersions: new[] { expectedVersion },
                defaultVersion: expectedVersion,
                pythonScriptGeneratorOptions: pythonScriptGeneratorOptions);
            var sourceDir = IOHelpers.CreateTempDir(_tempDirRoot);

            IOHelpers.CreateFile(sourceDir, "", "app.py");
            IOHelpers.CreateFile(sourceDir, "", PythonConstants.RequirementsFileName);
            IOHelpers.CreateFile(sourceDir, $"python-{runtimeTextFileVersion}", PythonConstants.RuntimeFileName);
            var repo    = new LocalSourceRepo(sourceDir, NullLoggerFactory.Instance);
            var context = CreateContext(repo);

            // Act
            var result = platform.Detect(context);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(PythonConstants.PlatformName, result.Platform);
            Assert.Equal(expectedVersion, result.PlatformVersion);
        }
Beispiel #3
0
        public void Detect_Throws_WhenUnsupportedPythonVersion_IsSetInOptions()
        {
            // Arrange
            var optionsVersion               = "100.100.100";
            var detectedVersion              = "1.2.3";
            var supportedVersion             = "1.2.3";
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions()
            {
                PythonVersion = optionsVersion
            };
            var platform = CreatePlatform(
                supportedVersions: new[] { supportedVersion },
                defaultVersion: supportedVersion,
                detectedVersion: detectedVersion,
                pythonScriptGeneratorOptions: pythonScriptGeneratorOptions);
            var context = CreateContext();

            // Act & Assert
            var exception = Assert.Throws <UnsupportedVersionException>(() => platform.Detect(context));

            Assert.Equal(
                $"Platform 'python' version '{optionsVersion}' is unsupported. " +
                $"Supported versions: {supportedVersion}",
                exception.Message);
        }
        public void Detect_Throws_WhenUnsupportedPythonVersion_IsSetInOptions()
        {
            // Arrange
            var unsupportedVersion           = "100.100.100";
            var supportedVersion             = "1.2.3";
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions()
            {
                PythonVersion = unsupportedVersion
            };
            var platform = CreatePlatform(
                supportedVersions: new[] { supportedVersion },
                defaultVersion: supportedVersion,
                pythonScriptGeneratorOptions: pythonScriptGeneratorOptions);
            var sourceDir = IOHelpers.CreateTempDir(_tempDirRoot);

            IOHelpers.CreateFile(sourceDir, "", PythonConstants.RequirementsFileName);
            IOHelpers.CreateFile(sourceDir, "python-" + supportedVersion, PythonConstants.RuntimeFileName);
            var repo    = new LocalSourceRepo(sourceDir, NullLoggerFactory.Instance);
            var context = CreateContext(repo);

            // Act & Assert
            var exception = Assert.Throws <UnsupportedVersionException>(() => platform.Detect(context));

            Assert.Equal(
                $"Platform 'python' version '{unsupportedVersion}' is unsupported. " +
                $"Supported versions: {supportedVersion}",
                exception.Message);
        }
        private PythonPlatformDetector CreatePythonPlatformDetector(
            PythonScriptGeneratorOptions pythonScriptGeneratorOptions)
        {
            pythonScriptGeneratorOptions = pythonScriptGeneratorOptions ?? new PythonScriptGeneratorOptions();

            return(new PythonPlatformDetector(
                       Options.Create(pythonScriptGeneratorOptions),
                       NullLogger <PythonPlatformDetector> .Instance,
                       new DefaultStandardOutputWriter()));
        }
        private PythonLanguageDetector CreatePythonLanguageDetector(
            string[] supportedPythonVersions,
            IEnvironment environment)
        {
            var optionsSetup = new PythonScriptGeneratorOptionsSetup(environment);
            var options      = new PythonScriptGeneratorOptions();

            optionsSetup.Configure(options);

            return(new PythonLanguageDetector(Options.Create(options), new TestPythonVersionProvider(supportedPythonVersions), NullLogger <PythonLanguageDetector> .Instance));
        }
        private PythonPlatformDetector CreatePythonPlatformDetector(
            string[] supportedPythonVersions,
            string defaultVersion,
            PythonScriptGeneratorOptions pythonScriptGeneratorOptions)
        {
            pythonScriptGeneratorOptions = pythonScriptGeneratorOptions ?? new PythonScriptGeneratorOptions();

            return(new PythonPlatformDetector(
                       new TestPythonVersionProvider(supportedPythonVersions, defaultVersion),
                       Options.Create(pythonScriptGeneratorOptions),
                       NullLogger <PythonPlatformDetector> .Instance,
                       new DefaultStandardOutputWriter()));
        }
        public void Configure_SetsPythonVersion_ToLatestVersion_IfNoEnvironmentVariable_IsSet()
        {
            // Arrange
            var environment  = new TestEnvironment();
            var optionsSetup = new PythonScriptGeneratorOptionsSetup(environment);
            var options      = new PythonScriptGeneratorOptions();

            // Act
            optionsSetup.Configure(options);

            // Assert
            Assert.Equal(Common.PythonVersions.Python37Version, options.PythonDefaultVersion);
        }
Beispiel #9
0
 private PythonPlatform CreatePlatform(
     IPythonVersionProvider pythonVersionProvider,
     PythonPlatformInstaller platformInstaller,
     BuildScriptGeneratorOptions commonOptions,
     PythonScriptGeneratorOptions pythonScriptGeneratorOptions)
 {
     return(new PythonPlatform(
                Options.Create(commonOptions),
                Options.Create(pythonScriptGeneratorOptions),
                pythonVersionProvider,
                NullLogger <PythonPlatform> .Instance,
                detector: null,
                platformInstaller));
 }
        public void Configure_SetsPythonVersion_ToEnvironmentVariableValue()
        {
            // Arrange
            var environment = new TestEnvironment();

            environment.Variables[PythonConstants.PythonVersionEnvVarName] = "10.10.10";
            var optionsSetup = new PythonScriptGeneratorOptionsSetup(environment);
            var options      = new PythonScriptGeneratorOptions();

            // Act
            optionsSetup.Configure(options);

            // Assert
            Assert.Equal("10.10.10", options.PythonVersion);
        }
Beispiel #11
0
        private PythonLanguageDetector CreatePythonLanguageDetector(
            string[] supportedPythonVersions,
            string defaultVersion,
            IEnvironment environment)
        {
            var optionsSetup = new PythonScriptGeneratorOptionsSetup(environment);
            var options      = new PythonScriptGeneratorOptions();

            optionsSetup.Configure(options);

            return(new PythonLanguageDetector(
                       new TestPythonVersionProvider(supportedPythonVersions, defaultVersion),
                       NullLogger <PythonLanguageDetector> .Instance,
                       new DefaultStandardOutputWriter()));
        }
Beispiel #12
0
        public void GeneratedSnippet_HaveInstallScript_IfCustomRequirementsTxtPathSpecified()
        {
            // Arrange
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions()
            {
                CustomRequirementsTxtPath = "foo/requirements.txt"
            };
            var commonOptions = new BuildScriptGeneratorOptions()
            {
                EnableDynamicInstall      = true,
                CustomRequirementsTxtPath = "foo/requirements.txt"
            };
            var installerScriptSnippet = "##INSTALLER_SCRIPT##";
            var versionProvider        = new TestPythonVersionProvider(new[] { "3.7.5", "3.8.0" }, defaultVersion: "3.7.5");
            var platformInstaller      = new TestPythonPlatformInstaller(
                isVersionAlreadyInstalled: false,
                installerScript: installerScriptSnippet,
                Options.Create(commonOptions),
                NullLoggerFactory.Instance);
            var platform = CreatePlatform(
                versionProvider,
                platformInstaller,
                commonOptions,
                pythonScriptGeneratorOptions);
            var repo = new MemorySourceRepo();

            repo.AddFile("", "foo/requirements.txt");
            repo.AddFile("print(1)", "bla.py");
            var context = new BuildScriptGeneratorContext {
                SourceRepo = repo
            };
            var detectorResult = new PythonPlatformDetectorResult
            {
                Platform               = PythonConstants.PlatformName,
                PlatformVersion        = "3.7.5",
                HasRequirementsTxtFile = true,
            };

            // Act
            var snippet = platform.GetInstallerScriptSnippet(context, detectorResult);

            // Assert
            Assert.NotNull(snippet);
        }
Beispiel #13
0
        public void GeneratedSnippet_HasInstallationScript_IfDynamicInstallIsEnabled()
        {
            // Arrange
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions();
            var commonOptions = new BuildScriptGeneratorOptions()
            {
                EnableDynamicInstall = true
            };
            var installerScriptSnippet = "##INSTALLER_SCRIPT##";
            var versionProvider        = new TestPythonVersionProvider(new[] { "3.7.5", "3.8.0" }, defaultVersion: "3.7.5");
            var platformInstaller      = new TestPythonPlatformInstaller(
                isVersionAlreadyInstalled: false,
                installerScript: installerScriptSnippet,
                Options.Create(commonOptions),
                NullLoggerFactory.Instance);
            var platform = CreatePlatform(
                versionProvider,
                platformInstaller,
                commonOptions,
                pythonScriptGeneratorOptions);
            var repo = new MemorySourceRepo();

            repo.AddFile("", PythonConstants.RequirementsFileName);
            repo.AddFile("print(1)", "bla.py");
            var context = new BuildScriptGeneratorContext {
                SourceRepo = repo
            };

            context.ResolvedPythonVersion = "3.7.5";

            // Act
            var snippet = platform.GenerateBashBuildScriptSnippet(context);

            // Assert
            Assert.NotNull(snippet);
            Assert.NotNull(snippet.PlatformInstallationScriptSnippet);
            Assert.Equal(installerScriptSnippet, snippet.PlatformInstallationScriptSnippet);
            Assert.Contains(ManifestFilePropertyKeys.PythonVersion, snippet.BuildProperties.Keys);
            Assert.Equal("3.7.5", snippet.BuildProperties[ManifestFilePropertyKeys.PythonVersion]);
        }
Beispiel #14
0
        public void Detect_ReturnsVersionFromOptions_EvenIfDetectorReturnsAVersion()
        {
            // Arrange
            var expectedVersion = "1.2.3";
            var detectedVersion = "2.5.0";
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions()
            {
                PythonVersion = expectedVersion
            };
            var platform = CreatePlatform(
                supportedVersions: new[] { expectedVersion },
                defaultVersion: expectedVersion,
                detectedVersion: detectedVersion,
                pythonScriptGeneratorOptions: pythonScriptGeneratorOptions);
            var context = CreateContext();

            // Act
            var result = platform.Detect(context);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(PythonConstants.PlatformName, result.Platform);
            Assert.Equal(expectedVersion, result.PlatformVersion);
        }