コード例 #1
0
ファイル: PropsTests.cs プロジェクト: wli3/insertions-client
        public void TestDownloadTimeout()
        {
            // Generate test default.config
            string testConfig        = @"<?xml version=""1.0"" encoding=""us-ascii""?>
<corext>
	<packages>
		<package id=""runtime.win-x64.Microsoft.NETCore.DotNetAppHost"" version=""unimportant"" link=""path\to\extract"" />
	</packages>
</corext>";
            string defaultConfigPath = Path.GetTempFileName();

            File.WriteAllText(defaultConfigPath, testConfig);

            // Load default.config
            DefaultConfigUpdater defaultConfigUpdater = new DefaultConfigUpdater();
            bool configLoadResult = defaultConfigUpdater.TryLoad(defaultConfigPath, out string error);

            Assert.IsTrue(configLoadResult, "Loading default.config failed.");

            // Load msi.swr
            string        swrPath       = Path.Combine(Environment.CurrentDirectory, "Assets", "msi.swr");
            SwrFileReader swrFileReader = new SwrFileReader(4);

            SwrFile[] loadedSwrFiles = swrFileReader.LoadSwrFiles(Environment.CurrentDirectory);
            SwrFile   swrFile        = loadedSwrFiles.First(s => s.Path == swrPath);

            PropsVariableDeducer variableDeducer = new PropsVariableDeducer("https://api.nuget.org/v3/index.json", null);
            bool operationResult = variableDeducer.DeduceVariableValues(defaultConfigUpdater,
                                                                        new[] { new PackageUpdateResult("runtime.win-x64.Microsoft.NETCore.DotNetAppHost", "unimportant", "3.1.3") },
                                                                        new SwrFile[] { swrFile }, out List <PropsFileVariableReference> results, out string details,

                                                                        // Timeout in zero seconds
                                                                        TimeSpan.FromSeconds(0));

            Assert.IsFalse(operationResult, "Operation should have timed out, but it succeeded.");
            Assert.IsTrue(details.Contains("timed out"));
            Assert.IsTrue(results == null || results.Count == 0, $"No props file should have been updated, {results?.Count} were updated");
        }
コード例 #2
0
ファイル: PropsTests.cs プロジェクト: wli3/insertions-client
        public void TestVariableValueDeduce(int maxWaitMilliseconds)
        {
            // Generate test default.config
            string testConfig        = @"<?xml version=""1.0"" encoding=""us-ascii""?>
<corext>
	<packages>
		<package id=""runtime.win-x64.Microsoft.NETCore.DotNetAppHost"" version=""unimportant"" link=""path\to\extract"" />
	</packages>
</corext>";
            string defaultConfigPath = Path.GetTempFileName();

            File.WriteAllText(defaultConfigPath, testConfig);

            // Load default.config
            DefaultConfigUpdater defaultConfigUpdater = new DefaultConfigUpdater();
            bool configLoadResult = defaultConfigUpdater.TryLoad(defaultConfigPath, out string error);

            Assert.IsTrue(configLoadResult, "Loading default.config failed.");

            // Load msi.swr
            string        swrPath       = Path.Combine(Environment.CurrentDirectory, "Assets", "msi.swr");
            SwrFileReader swrFileReader = new SwrFileReader(4);

            SwrFile[] loadedSwrFiles = swrFileReader.LoadSwrFiles(Environment.CurrentDirectory);
            SwrFile   swrFile        = loadedSwrFiles.First(s => s.Path == swrPath);

            PropsVariableDeducer variableDeducer = new PropsVariableDeducer("https://api.nuget.org/v3/index.json", null);
            bool operationResult = variableDeducer.DeduceVariableValues(defaultConfigUpdater,
                                                                        new[] { new PackageUpdateResult("runtime.win-x64.Microsoft.NETCore.DotNetAppHost", "unimportant", "3.1.3") },
                                                                        new SwrFile[] { swrFile }, out List <PropsFileVariableReference> results, out string details, TimeSpan.FromMilliseconds(maxWaitMilliseconds));

            Assert.IsTrue(operationResult, $"Operation failed with message: {details}");
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Any(r => r.ReferencedFilePath == swrPath), "Cannot find the value of variable in given swr.");
            Assert.IsTrue(results.Any(r => r.ReferencedFilePath == swrPath && r.Name == "AspNetCoreTargetingPack30Version"), "Cannot find the correct variable.");
            Assert.IsTrue(results.Any(r => r.ReferencedFilePath == swrPath && r.Name == "AspNetCoreTargetingPack30Version" && r.Value == "apphost"), "Wrong value was found for the variable.");
        }
コード例 #3
0
        public UpdateResults UpdateVersions(
            IEnumerable <string> manifestFiles,
            string defaultConfigFile,
            IEnumerable <Regex> whitelistedPackages,
            ImmutableHashSet <string>?packagesToIgnore,
            string?accessToken             = null,
            string?propsFilesRootDirectory = null,
            Predicate <Build>?buildFilter  = null)
        {
            List <Asset>         assets = new List <Asset>();
            DefaultConfigUpdater configUpdater;

            if (!TryLoadDefaultConfig(defaultConfigFile, out configUpdater, out string details))
            {
                return(new UpdateResults {
                    OutcomeDetails = details, IgnoredNuGets = packagesToIgnore
                });
            }

            foreach (var manifestFile in manifestFiles)
            {
                if (!TryValidateManifestFile(manifestFile, out details) ||
                    !TryExtractManifestAssets(manifestFile, buildFilter, assets, out details))
                {
                    return(new UpdateResults {
                        OutcomeDetails = details, IgnoredNuGets = packagesToIgnore
                    });
                }
            }

            UpdateResults results = new UpdateResults
            {
                IgnoredNuGets = packagesToIgnore
            };

            Stopwatch overallRunStopWatch = Stopwatch.StartNew();

            using CancellationTokenSource source = new CancellationTokenSource(_maxWaitDuration);
            try
            {
                _ = Parallel.ForEach(assets,
                                     CreateParallelOptions(source.Token),
                                     asset => ParallelCallback(asset, whitelistedPackages, packagesToIgnore, configUpdater, results));

                /* Delay saving config file changes until props file updates are successful.
                 * If we save the results now and props-file step fails, re-running the application won't attempt to update props files again.
                 * A partial success in the app shouldn't hide the errors in the consecutive runs. */

                // Only update props files if user specified an access token. Null token means user doesn't want to update props files.
                bool propsUpdatesEnabled = accessToken != null;

                if (propsUpdatesEnabled)
                {
                    // Attempt to find a proper directory to search for props files, if we are not already given one.
                    if (string.IsNullOrWhiteSpace(propsFilesRootDirectory))
                    {
                        if (FindPropsFileRootDirectory(defaultConfigFile, out propsFilesRootDirectory))
                        {
                            Trace.WriteLine($"The directory to search for .props files: {propsFilesRootDirectory}");
                        }
                        else
                        {
                            Trace.WriteLine("Failed to find an appropriate folder to search for .props files.");

                            results.PropsFileUpdateResults = new PropsUpdateResults()
                            {
                                Outcome        = false,
                                OutcomeDetails = "Failed to find an appropriate folder to search for .props files."
                            };
                        }
                    }

                    // Update props files if we have a valid directory to search
                    if (!string.IsNullOrWhiteSpace(propsFilesRootDirectory))
                    {
                        SwrFileReader swrFileReader = new SwrFileReader(_maxConcurrentWorkers);
                        SwrFile[]     swrFiles      = swrFileReader.LoadSwrFiles(propsFilesRootDirectory);

                        PropsVariableDeducer variableDeducer = new PropsVariableDeducer(InsertionConstants.DefaultNugetFeed, accessToken);
                        bool deduceOperationResult           = variableDeducer.DeduceVariableValues(configUpdater, results.UpdatedNuGets,
                                                                                                    swrFiles, out List <PropsFileVariableReference> variables, out string outcomeDetails, _maxDownloadDuration);

                        PropsFileUpdater propsFileUpdater = new PropsFileUpdater();
                        results.PropsFileUpdateResults = propsFileUpdater.UpdatePropsFiles(variables, propsFilesRootDirectory);

                        if (!deduceOperationResult)
                        {
                            results.PropsFileUpdateResults.Outcome         = false;
                            results.PropsFileUpdateResults.OutcomeDetails += outcomeDetails;
                        }
                    }
                }
                else
                {
                    Trace.WriteLine(".props file updates are skipped since no access token was specified.");
                }

                if (!propsUpdatesEnabled || results.PropsFileUpdateResults !.Outcome == true)
                {
                    // Prop files were updated successfuly. It is safe to save config update results.
                    results.FileSaveResults = configUpdater.Save();
                }
                else
                {
                    Trace.WriteLine("default.config and .packageconfig file updates were skipped, because " +
                                    "there was an issue updating .props files.");
                    results.FileSaveResults = new FileSaveResult[0];
                    results.OutcomeDetails += "Failure in updating .props files.";
                }
            }