Exemple #1
0
        public int RunTests(DotnetTestParams dotnetTestParams)
        {
            var projectPath        = GetProjectPath(dotnetTestParams.ProjectOrAssemblyPath);
            var runtimeIdentifiers = !string.IsNullOrEmpty(dotnetTestParams.Runtime)
                ? new[] { dotnetTestParams.Runtime }
                : RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers();
            var exitCode = 0;

            // Create a workspace
            var workspace = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment());

            if (dotnetTestParams.Framework != null)
            {
                var projectContext = workspace.GetProjectContext(projectPath, dotnetTestParams.Framework);
                if (projectContext == null)
                {
                    Reporter.Error.WriteLine(
                        $"Project '{projectPath}' does not support framework: {dotnetTestParams.UnparsedFramework}");
                    return(1);
                }
                projectContext = workspace.GetRuntimeContext(projectContext, runtimeIdentifiers);

                exitCode = RunTests(projectContext, dotnetTestParams);
            }
            else
            {
                var summary         = new Summary();
                var projectContexts = workspace.GetProjectContextCollection(projectPath)
                                      .EnsureValid(projectPath)
                                      .FrameworkOnlyContexts
                                      .Select(c => workspace.GetRuntimeContext(c, runtimeIdentifiers))
                                      .ToList();

                // Execute for all TFMs the project targets.
                foreach (var projectContext in projectContexts)
                {
                    var result = RunTests(projectContext, dotnetTestParams);
                    if (result == 0)
                    {
                        summary.Passed++;
                    }
                    else
                    {
                        summary.Failed++;
                        if (exitCode == 0)
                        {
                            // If tests fail in more than one TFM, we'll have it use the result of the first one
                            // as the exit code.
                            exitCode = result;
                        }
                    }
                }

                summary.Print();
            }

            return(exitCode);
        }
        public static void Main(string[] args)
        {
            try
            {
                string projectJsonFileName = Path.GetFullPath("project.json");

                string projectDirectory = System.IO.Path.GetDirectoryName(projectJsonFileName);

                var workspace         = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment());
                var frameworkContexts = workspace.GetProjectContextCollection(projectDirectory)
                                        .FrameworkOnlyContexts;

                var rids = RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers();

                foreach (var frameworkContext in frameworkContexts)
                {
                    var runtimeContext = workspace.GetRuntimeContext(frameworkContext, rids);

                    OutputPaths paths = runtimeContext.GetOutputPaths("Debug");
                    Console.WriteLine("TargetFramework: " + frameworkContext.Identity.TargetFramework);
                    Console.WriteLine("Executable: " + paths.RuntimeFiles.Executable);
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        private ProjectContext GetRuntimeContext()
        {
            var            workspace       = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment());
            var            projectContexts = ProjectContext.CreateContextForEachFramework(ProjectPath).ToArray();
            ProjectContext projectContext;

            if (TargetFramework != null)
            {
                projectContext = projectContexts.FirstOrDefault(context => context.TargetFramework.Equals(TargetFramework));
                if (projectContext == null)
                {
                    throw new InvalidOperationException($"Project '{ProjectPath}' does not support framework: {FrameworkOption.Value()}");
                }
            }
            else if (projectContexts.Length == 1)
            {
                projectContext = projectContexts[0];
            }
            else
            {
                throw new InvalidOperationException($"Project '{ProjectPath}' targets multiple frameworks. Specify one using '{FrameworkOption.Template}.");
            }

            return(workspace.GetRuntimeContext(
                       projectContext,
                       RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers()));
        }
Exemple #4
0
        private CompilerIO ComputeIO(ProjectGraphNode graphNode)
        {
            var inputs  = new List <string>();
            var outputs = new List <string>();

            var isRootProject = graphNode.IsRoot;
            var project       = graphNode.ProjectContext;

            var calculator         = project.GetOutputPaths(_configuration, _buildBasePath, _outputPath);
            var binariesOutputPath = calculator.CompilationOutputPath;
            var compilerOptions    = project.ProjectFile.GetCompilerOptions(project.TargetFramework, _configuration);

            // input: project.json
            inputs.Add(project.ProjectFile.ProjectFilePath);

            // input: lock file; find when dependencies change
            AddLockFile(project, inputs);

            // input: source files
            inputs.AddRange(CompilerUtil.GetCompilationSources(project, compilerOptions));

            var allOutputPath = new HashSet <string>(calculator.CompilationFiles.All());

            if (isRootProject && project.ProjectFile.HasRuntimeOutput(_configuration))
            {
                var runtimeContext = _workspace.GetRuntimeContext(project, _runtimes);
                foreach (var path in runtimeContext.GetOutputPaths(_configuration, _buildBasePath, _outputPath).RuntimeFiles.All())
                {
                    allOutputPath.Add(path);
                }
            }
            foreach (var dependency in graphNode.Dependencies)
            {
                var outputFiles = dependency.ProjectContext
                                  .GetOutputPaths(_configuration, _buildBasePath, _outputPath)
                                  .CompilationFiles;

                inputs.Add(outputFiles.Assembly);
            }

            // output: compiler outputs
            foreach (var path in allOutputPath)
            {
                outputs.Add(path);
            }

            // input compilation options files
            AddCompilationOptions(project, _configuration, inputs);

            // input / output: resources with culture
            AddNonCultureResources(project, calculator.IntermediateOutputDirectoryPath, inputs, outputs, compilerOptions);

            // input / output: resources without culture
            AddCultureResources(project, binariesOutputPath, inputs, outputs, compilerOptions);

            return(new CompilerIO(inputs, outputs));
        }
Exemple #5
0
        private ProjectContext GetRuntimeContext()
        {
            var workspace = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment());

            var projectContext = workspace.GetProjectContext(ProjectPath, TargetFramework);

            if (projectContext == null)
            {
                Debug.Assert(FrameworkOption.HasValue());
                throw new InvalidOperationException($"Project '{ProjectPath}' does not support framework: {FrameworkOption.Value()}");
            }

            var runtimeContext = workspace.GetRuntimeContext(
                projectContext,
                RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());

            return(runtimeContext);
        }
        string ResolveOutputPath(
            IEnumerable <ProjectContext> frameworkContexts,
            BuildWorkspace workspace,
            string configuration,
            IEnumerable <string> rids)
        {
            foreach (ProjectContext frameworkContext in frameworkContexts)
            {
                ProjectContext runtimeContext = workspace.GetRuntimeContext(frameworkContext, rids);

                OutputPaths paths = runtimeContext.GetOutputPaths(configuration);
                if (File.Exists(paths.RuntimeFiles.Executable))
                {
                    return(paths.RuntimeFiles.Executable);
                }
            }

            return(null);
        }
Exemple #7
0
        private ScriptVariablesFixture(string rid)
        {
            var projectJson = Path.Combine(TestAssetPath, "project.json");
            var command     = new Mock <ICommand>();

            command.Setup(c => c.Execute()).Returns(new CommandResult());
            command.Setup(c => c.WorkingDirectory(It.IsAny <string>())).Returns(() => command.Object);
            command.Setup(c => c.OnErrorLine(It.IsAny <Action <string> >())).Returns(() => command.Object);
            command.Setup(c => c.OnOutputLine(It.IsAny <Action <string> >())).Returns(() => command.Object);
            var commandFactory = new Mock <ICommandFactory>();

            commandFactory.Setup(c => c
                                 .Create(
                                     It.IsAny <string>(),
                                     It.IsAny <IEnumerable <string> >(),
                                     It.IsAny <NuGetFramework>(),
                                     It.IsAny <string>()))
            .Returns(command.Object);

            var _args = new BuildCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform", new BuildWorkspace(new ProjectReaderSettings()));

            _args.ConfigValue = ConfigValue;

            PreCompileScriptVariables  = new Dictionary <string, string>();
            PostCompileScriptVariables = new Dictionary <string, string>();

            var _scriptRunner = new Mock <IScriptRunner>();

            _scriptRunner.Setup(
                s =>
                s.RunScripts(It.IsAny <ProjectContext>(), It.IsAny <string>(), It.IsAny <Dictionary <string, string> >()))
            .Callback <ProjectContext, string, Dictionary <string, string> >((p, n, v) =>
            {
                if (n.Equals(ScriptNames.PreCompile))
                {
                    PreCompileScriptVariables = v;
                }

                if (n.Equals(ScriptNames.PostCompile))
                {
                    PostCompileScriptVariables = v;
                }
            });

            var managedCompiler = new ManagedCompiler(_scriptRunner.Object, commandFactory.Object);

            var rids = new List <string>();

            if (!string.IsNullOrEmpty(rid))
            {
                rids.Add(rid);
            }

            var workspace = new BuildWorkspace(new ProjectReaderSettings());
            var context   = workspace.GetRuntimeContext(workspace.GetProjectContext(projectJson, TestAssetFramework), rids);

            context = workspace.GetRuntimeContext(context, rids);
            managedCompiler.Compile(context, _args);

            RuntimeOutputDir = Path.Combine(OutputPath, rid);
        }
Exemple #8
0
        public int DoRun(string[] args)
        {
            DebugHelper.HandleDebugSwitch(ref args);

            var dotnetTestParams = new DotnetTestParams();

            try
            {
                dotnetTestParams.Parse(args);

                if (dotnetTestParams.Help)
                {
                    return(0);
                }

                // Register for parent process's exit event
                if (dotnetTestParams.ParentProcessId.HasValue)
                {
                    RegisterForParentProcessExit(dotnetTestParams.ParentProcessId.Value);
                }

                var projectPath        = GetProjectPath(dotnetTestParams.ProjectPath);
                var runtimeIdentifiers = !string.IsNullOrEmpty(dotnetTestParams.Runtime) ?
                                         new[] { dotnetTestParams.Runtime } :
                RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers();
                var exitCode = 0;

                // Create a workspace
                var workspace = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment());

                if (dotnetTestParams.Framework != null)
                {
                    var projectContext = workspace.GetProjectContext(projectPath, dotnetTestParams.Framework);
                    if (projectContext == null)
                    {
                        Reporter.Error.WriteLine($"Project '{projectPath}' does not support framework: {dotnetTestParams.UnparsedFramework}");
                        return(1);
                    }
                    projectContext = workspace.GetRuntimeContext(projectContext, runtimeIdentifiers);

                    exitCode = RunTest(projectContext, dotnetTestParams, workspace);
                }
                else
                {
                    var summary         = new Summary();
                    var projectContexts = workspace.GetProjectContextCollection(projectPath)
                                          .EnsureValid(projectPath)
                                          .FrameworkOnlyContexts
                                          .Select(c => workspace.GetRuntimeContext(c, runtimeIdentifiers))
                                          .ToList();

                    // Execute for all TFMs the project targets.
                    foreach (var projectContext in projectContexts)
                    {
                        var result = RunTest(projectContext, dotnetTestParams, workspace);
                        if (result == 0)
                        {
                            summary.Passed++;
                        }
                        else
                        {
                            summary.Failed++;
                            if (exitCode == 0)
                            {
                                // If tests fail in more than one TFM, we'll have it use the result of the first one
                                // as the exit code.
                                exitCode = result;
                            }
                        }
                    }

                    summary.Print();
                }

                return(exitCode);
            }
            catch (InvalidOperationException ex)
            {
                TestHostTracing.Source.TraceEvent(TraceEventType.Error, 0, ex.ToString());
                return(-1);
            }
            catch (Exception ex) when(!(ex is GracefulException))
            {
                TestHostTracing.Source.TraceEvent(TraceEventType.Error, 0, ex.ToString());
                return(-2);
            }
        }