Пример #1
0
        public int Run(RebaseUriOptions rebaseOptions)
        {
            try
            {
                if (!Uri.TryCreate(rebaseOptions.BasePath, UriKind.Absolute, out Uri baseUri))
                {
                    Console.Error.WriteLine($"The value '{rebaseOptions.BasePath}' of the --base-path-value option is not an absolute URI.");
                    return(FAILURE);
                }

                // In case someone accidentally passes C:\bld\src and meant C:\bld\src\--the base path should always be a folder, not something that points to a file.
                if (!string.IsNullOrEmpty(baseUri.GetFileName()))
                {
                    baseUri = new Uri(baseUri.ToString() + "/");
                }

                IEnumerable <RebaseUriFile> rebaseUriFiles = GetRebaseUriFiles(rebaseOptions);

                if (!ValidateOptions(rebaseOptions, rebaseUriFiles))
                {
                    return(FAILURE);
                }

                if (!rebaseOptions.Inline)
                {
                    _fileSystem.CreateDirectory(rebaseOptions.OutputFolderPath);
                }

                Formatting formatting = rebaseOptions.PrettyPrint
                    ? Formatting.Indented
                    : Formatting.None;

                OptionallyEmittedData dataToRemove = rebaseOptions.DataToRemove.ToFlags();
                OptionallyEmittedData dataToInsert = rebaseOptions.DataToInsert.ToFlags();

                foreach (RebaseUriFile rebaseUriFile in rebaseUriFiles)
                {
                    if (dataToRemove != 0)
                    {
                        rebaseUriFile.Log = new RemoveOptionalDataVisitor(dataToRemove).VisitSarifLog(rebaseUriFile.Log);
                    }

                    if (dataToInsert != 0)
                    {
                        rebaseUriFile.Log = new InsertOptionalDataVisitor(dataToInsert).VisitSarifLog(rebaseUriFile.Log);
                    }

                    rebaseUriFile.Log = rebaseUriFile.Log.RebaseUri(rebaseOptions.BasePathToken, rebaseOptions.RebaseRelativeUris, baseUri);

                    WriteSarifFile(_fileSystem, rebaseUriFile.Log, rebaseUriFile.OutputFilePath, formatting);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(FAILURE);
            }

            return(SUCCESS);
        }
Пример #2
0
        public void RebaseUriCommand_RebaseRunWithArtifacts()
        {
            string testFilePath = "RunWithArtifacts.sarif";

            this.options = CreateDefaultOptions();

            RunTest(testFilePath);
        }
Пример #3
0
        private bool ValidateOptions(RebaseUriOptions rebaseOptions, IEnumerable <RebaseUriFile> rebaseUriFiles)
        {
            bool valid = true;

            valid &= rebaseOptions.ValidateOutputOptions();

            valid &= DriverUtilities.ReportWhetherOutputFilesCanBeCreated(rebaseUriFiles.Select(f => f.OutputFilePath), rebaseOptions.Force, _fileSystem);

            return(valid);
        }
Пример #4
0
            internal string GetOutputFileName(RebaseUriOptions rebaseUriOptions)
            {
                if (rebaseUriOptions.Inline)
                {
                    return(FileName);
                }

                return(!string.IsNullOrEmpty(rebaseUriOptions.OutputFolderPath)
                    ? Path.GetFullPath(rebaseUriOptions.OutputFolderPath) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(FileName) + "-rebased.sarif"
                    : Path.GetDirectoryName(FileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(FileName) + "-rebased.sarif");
            }
Пример #5
0
        private IEnumerable <RebaseUriFile> GetSarifFiles(RebaseUriOptions mergeOptions)
        {
            // Get files names first, as we may write more sarif logs to the same directory as we rebase them.
            HashSet <string> fileNames = FileHelpers.CreateTargetsSet(mergeOptions.TargetFileSpecifiers, mergeOptions.Recurse);

            foreach (var file in fileNames)
            {
                yield return(new RebaseUriFile()
                {
                    FileName = file, Log = FileHelpers.ReadSarifFile <SarifLog>(_fileSystem, file)
                });
            }
        }
Пример #6
0
        public void RebaseUriCommand_RebaseRunWithArtifacts()
        {
            string testFilePath = "RunWithArtifacts.sarif";

            this.options = new RebaseUriOptions
            {
                BasePath           = @"C:\vs\src\2\s\",
                BasePathToken      = "SRCROOT",
                Inline             = true,
                SarifOutputVersion = SarifVersion.Current,
                PrettyPrint        = true
            };

            RunTest(testFilePath);
        }
Пример #7
0
        private IEnumerable <RebaseUriFile> GetRebaseUriFiles(RebaseUriOptions rebaseUriOptions)
        {
            // Get files names first, as we may write more sarif logs to the same directory as we rebase them.
            HashSet <string> inputFilePaths = CreateTargetsSet(rebaseUriOptions.TargetFileSpecifiers, rebaseUriOptions.Recurse, _fileSystem);

            foreach (var inputFilePath in inputFilePaths)
            {
                yield return(new RebaseUriFile
                {
                    InputFilePath = inputFilePath,
                    OutputFilePath = GetOutputFilePath(inputFilePath, rebaseUriOptions),
                    Log = ReadSarifFile <SarifLog>(_fileSystem, inputFilePath)
                });
            }
        }
Пример #8
0
        public int Run(RebaseUriOptions rebaseOptions)
        {
            try
            {
                Uri baseUri;
                if (!Uri.TryCreate(rebaseOptions.BasePath, UriKind.Absolute, out baseUri))
                {
                    throw new ArgumentException($"BasePath {rebaseOptions.BasePath} was not an absolute URI.  It must be.");
                }

                // In case someone accidentally passes C:\bld\src and meant C:\bld\src\--the base path should always be a folder, not something that points to a file.
                if (!string.IsNullOrEmpty(baseUri.GetFileName()))
                {
                    baseUri = new Uri(baseUri.ToString() + "/");
                }

                var sarifFiles = GetSarifFiles(rebaseOptions);

                if (!rebaseOptions.Inline)
                {
                    Directory.CreateDirectory(rebaseOptions.OutputFolderPath);
                }

                foreach (var sarifLog in sarifFiles)
                {
                    sarifLog.Log = sarifLog.Log.RebaseUri(rebaseOptions.BasePathToken, rebaseOptions.RebaseRelativeUris, baseUri);

                    // Write output to file.
                    string outputName = sarifLog.GetOutputFileName(rebaseOptions);
                    var    formatting = rebaseOptions.PrettyPrint
                        ? Formatting.Indented
                        : Formatting.None;

                    WriteSarifFile(_fileSystem, sarifLog.Log, outputName, formatting);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(1);
            }

            return(0);
        }
Пример #9
0
        private string RunRebaseUriCommand(string testFilePath, RebaseUriOptions options)
        {
            string inputSarifLog = Extractor.GetResourceText($"RebaseUriCommand.{testFilePath}");

            string        logFilePath         = @"c:\logs\mylog.sarif";
            StringBuilder transformedContents = new StringBuilder();

            options.TargetFileSpecifiers = new string[] { logFilePath };

            Mock <IFileSystem> mockFileSystem = ArrangeMockFileSystem(inputSarifLog, logFilePath, transformedContents);

            var rebaseUriCommand = new RebaseUriCommand(mockFileSystem.Object);

            int    returnCode   = rebaseUriCommand.Run(options);
            string actualOutput = transformedContents.ToString();

            returnCode.Should().Be(0);

            return(actualOutput);
        }
 public void ConsumeEnvVarsAndInterpretOptions(RebaseUriOptions rebaseUriOptions)
 {
     ConsumeEnvVarsAndInterpretOptions((MultipleFilesOptionsBase)rebaseUriOptions);
 }
Пример #11
0
        public void RebaseUriCommand_InjectsRegions()
        {
            string productDirectory = FileDiffingFunctionalTests.GetProductDirectory();
            string analysisFile     = Path.Combine(productDirectory, @"ReleaseHistory.md");

            File.Exists(analysisFile).Should().BeTrue();

            var sarifLog = new SarifLog
            {
                Runs = new[]
                {
                    new Run {
                        Results = new[] {
                            new Result {
                                Locations = new [] {
                                    new Location {
                                        PhysicalLocation = new PhysicalLocation {
                                            Region = new Region {
                                                StartLine = 7
                                            },
                                            ArtifactLocation = new ArtifactLocation
                                            {
                                                Uri = new Uri(analysisFile)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            string inputSarifLog = JsonConvert.SerializeObject(sarifLog);

            string        logFilePath         = @"c:\logs\mylog.sarif";
            StringBuilder transformedContents = new StringBuilder();

            RebaseUriOptions options = CreateDefaultOptions();

            options.TargetFileSpecifiers = new string[] { logFilePath };

            options.DataToInsert = new[]
            {
                OptionallyEmittedData.RegionSnippets |
                OptionallyEmittedData.ContextRegionSnippets
            };

            Mock <IFileSystem> mockFileSystem = ArrangeMockFileSystem(inputSarifLog, logFilePath, transformedContents);

            // Test snippet injection.
            var rebaseUriCommand = new RebaseUriCommand(mockFileSystem.Object);

            int returnCode = rebaseUriCommand.Run(options);

            returnCode.Should().Be(0);

            SarifLog actualLog = JsonConvert.DeserializeObject <SarifLog>(transformedContents.ToString());

            actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.Region.Snippet.Should().NotBeNull();
            actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.ContextRegion.Snippet.Should().NotBeNull();

            // Now test that this data is removed.
            inputSarifLog = JsonConvert.SerializeObject(actualLog);
            transformedContents.Length = 0;
            mockFileSystem             = ArrangeMockFileSystem(inputSarifLog, logFilePath, transformedContents);
            rebaseUriCommand           = new RebaseUriCommand(mockFileSystem.Object);

            options.DataToRemove = options.DataToInsert;
            options.DataToInsert = null;

            returnCode = rebaseUriCommand.Run(options);
            returnCode.Should().Be(0);

            actualLog = JsonConvert.DeserializeObject <SarifLog>(transformedContents.ToString());
            actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.Region.Snippet.Should().BeNull();
            actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.ContextRegion.Snippet.Should().BeNull();
        }