public void CompilesProject(EnvironmentPreference preference, string solutionPath, string projectPath)
        {
            // Given
            StringWriter    log     = new StringWriter();
            AnalyzerManager manager = new AnalyzerManager(solutionPath, new AnalyzerManagerOptions
            {
                LogWriter = log
            });
            ProjectAnalyzer    analyzer = manager.GetProject(projectPath);
            EnvironmentOptions options  = new EnvironmentOptions
            {
                Preference = preference
            };

            // Set some enviornment variables to make it seem like we're not in a CI build
            // Sometimes this messes up libraries like SourceLink since we're building as part of a test and not for CI
            options.EnvironmentVariables.Add("APPVEYOR", "False");
            options.EnvironmentVariables.Add("ContinuousIntegrationBuild", null);
            options.EnvironmentVariables.Add("CI", "False");
            options.EnvironmentVariables.Add("CI_LINUX", "False");
            options.EnvironmentVariables.Add("CI_WINDOWS", "False");

            // When
            DeleteProjectDirectory(analyzer.ProjectFile.Path, "obj");
            DeleteProjectDirectory(analyzer.ProjectFile.Path, "bin");
            analyzer.IgnoreFaultyImports = false;

#pragma warning disable 0162
            if (BinaryLog)
            {
                analyzer.AddBinaryLogger($@"E:\Temp\{Path.GetFileNameWithoutExtension(solutionPath)}.{Path.GetFileNameWithoutExtension(analyzer.ProjectFile.Path)}.core.binlog");
            }
#pragma warning restore 0162

#if Is_Windows
            AnalyzerResults results = analyzer.Build(options);
#else
            // On non-Windows platforms we have to remove the .NET Framework target frameworks and only build .NET Core target frameworks
            // See https://github.com/dotnet/sdk/issues/826
            string[] excludedTargetFrameworks = new[] { "net2", "net3", "net4", "portable" };
            string[] targetFrameworks         = analyzer.ProjectFile.TargetFrameworks.Where(x => !excludedTargetFrameworks.Any(y => x.StartsWith(y))).ToArray();
            if (targetFrameworks.Length == 0)
            {
                Assert.Ignore();
            }
            AnalyzerResults results = analyzer.Build(targetFrameworks, options);
#endif

            // Then
            results.Count.ShouldBeGreaterThan(0, log.ToString());
            results.OverallSuccess.ShouldBeTrue(log.ToString());
            results.ShouldAllBe(x => x.Succeeded, log.ToString());
        }
示例#2
0
        private static Project GetProject()
        {
            var             projectPath = Path.GetFullPath(Path.Combine("..", "..", "..", "..", "Examples", "CommandTests", "CommandTests.csproj"));
            AnalyzerManager manager     = new AnalyzerManager();
            ProjectAnalyzer analyzer    = manager.GetProject(projectPath);
            AdhocWorkspace  workspace   = new AdhocWorkspace();

            analyzer.AddBinaryLogger("binarylogtests.binlog");
            var project = analyzer.AddToWorkspace(workspace);

            if (project is null || !project.HasDocuments)
            {
                throw new Exception("Error loading project, check binarylogtests.binlog file for errors.");
            }
            return(project);
        }
示例#3
0
        private Compilation AddProjectReferences(IExecutionContext context, List <ISymbol> symbols, Compilation compilation)
        {
            // Generate a single Workspace and add all of the projects to it
            StringWriter    log     = new StringWriter();
            AnalyzerManager manager = new AnalyzerManager(new AnalyzerManagerOptions
            {
                LogWriter = log
            });
            AdhocWorkspace      workspace    = new AdhocWorkspace();
            IEnumerable <IFile> projectFiles = context.FileSystem.GetInputFiles(_projectGlobs)
                                               .Where(x => x.Path.Extension == ".csproj" && x.Exists);
            List <Project> projects = new List <Project>();

            foreach (IFile projectFile in projectFiles)
            {
                Project project = workspace.CurrentSolution.Projects.FirstOrDefault(x => new FilePath(x.FilePath).Equals(projectFile.Path));
                if (project != null)
                {
                    Trace.Verbose($"Project {projectFile.Path.FullPath} was already in the workspace");
                }
                else
                {
                    Trace.Verbose($"Creating workspace project for {projectFile.Path.FullPath}");
                    ProjectAnalyzer analyzer = manager.GetProject(projectFile.Path.FullPath);
                    if (context.Bool(CodeAnalysisKeys.OutputBuildLog))
                    {
                        analyzer.AddBinaryLogger();
                    }
                    AnalyzerResult result = ReadWorkspace.CompileProjectAndTrace(analyzer, log);
                    if (result != null)
                    {
                        project = result.AddToWorkspace(workspace);
                        if (!project.Documents.Any())
                        {
                            Trace.Warning($"Project at {projectFile.Path.FullPath} contains no documents, which may be an error (check previous log output for any MSBuild warnings)");
                        }
                    }
                }
                projects.Add(project);
            }
            compilation = AddProjectReferences(projects, symbols, compilation);
            return(compilation);
        }
        /// <inheritdoc />
        protected override IEnumerable <Project> GetProjects(IExecutionContext context, IFile file)
        {
            StringWriter    log     = new StringWriter();
            AnalyzerManager manager = new AnalyzerManager(new AnalyzerManagerOptions
            {
                LogWriter = log
            });
            ProjectAnalyzer analyzer = manager.GetProject(file.Path.FullPath);

            if (context.Bool(CodeAnalysisKeys.OutputBuildLog))
            {
                analyzer.AddBinaryLogger();
            }
            AnalyzerResult result    = CompileProjectAndTrace(analyzer, log);
            AdhocWorkspace workspace = new AdhocWorkspace();

            result.AddToWorkspace(workspace);
            return(workspace.CurrentSolution.Projects);
        }
示例#5
0
        public void GetsSourceFilesFromBinaryLog(
            [ValueSource(nameof(Preferences))] EnvironmentPreference preference,
            [ValueSource(nameof(ProjectFiles))] string projectFile)
        {
            // Given
            StringWriter       log      = new StringWriter();
            ProjectAnalyzer    analyzer = GetProjectAnalyzer(projectFile, log);
            EnvironmentOptions options  = new EnvironmentOptions
            {
                Preference = preference
            };
            string binLogPath = Path.ChangeExtension(Path.GetTempFileName(), ".binlog");

            analyzer.AddBinaryLogger(binLogPath);

            try
            {
                // When
                analyzer.Build(options);
                IReadOnlyList <string> sourceFiles = analyzer.Manager.Analyze(binLogPath).First().SourceFiles;

                // Then
                sourceFiles.ShouldNotBeNull(log.ToString());
                new[]
                {
#if Is_Windows
                    // Linux and Mac builds appear to omit the AssemblyAttributes.cs file
                    "AssemblyAttributes",
#endif
                    "Class1",
                    "AssemblyInfo"
                }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
            }
            finally
            {
                if (File.Exists(binLogPath))
                {
                    File.Delete(binLogPath);
                }
            }
        }
        public void CompilesProject(EnvironmentPreference preference, string solutionPath, string projectPath)
        {
            // Given
            StringWriter    log     = new StringWriter();
            AnalyzerManager manager = new AnalyzerManager(solutionPath, new AnalyzerManagerOptions
            {
                LogWriter       = log,
                LoggerVerbosity = Verbosity
            });
            ProjectAnalyzer    analyzer = manager.GetProject(projectPath);
            EnvironmentOptions options  = new EnvironmentOptions
            {
                Preference = preference
            };

            // Set some enviornment variables to make it seem like we're not in a CI build
            // Sometimes this messes up libraries like SourceLink since we're building as part of a test and not for CI
            options.EnvironmentVariables.Add("APPVEYOR", "False");
            options.EnvironmentVariables.Add("ContinuousIntegrationBuild", null);
            options.EnvironmentVariables.Add("CI", "False");
            options.EnvironmentVariables.Add("CI_LINUX", "False");
            options.EnvironmentVariables.Add("CI_WINDOWS", "False");

            // When
            DeleteProjectDirectory(analyzer.ProjectFile.Path, "obj");
            DeleteProjectDirectory(analyzer.ProjectFile.Path, "bin");
            analyzer.IgnoreFaultyImports = false;
            if (BinaryLog)
            {
                analyzer.AddBinaryLogger($@"E:\Temp\{Path.GetFileNameWithoutExtension(solutionPath)}.{Path.GetFileNameWithoutExtension(analyzer.ProjectFile.Path)}.core.binlog");
            }
            AnalyzerResults results = analyzer.Build(options);

            // Then
            results.Count.ShouldBeGreaterThan(0, log.ToString());
            results.ShouldAllBe(x => x.OverallSuccess, log.ToString());
        }