Beispiel #1
0
        private IEnumerable <ProjectContext> SelectContexts(string projectPath, NuGetFramework framework, string runtime)
        {
            if (projectPath.EndsWith("project.json"))
            {
                if (File.Exists(projectPath) == false)
                {
                    throw new InvalidProjectException($"'{projectPath}' does not exist");
                }
            }
            else if (File.Exists(Path.Combine(projectPath, "project.json")) == false)
            {
                throw new InvalidProjectException($"'{projectPath}' does not contain a project.json file");
            }

            var contexts = Workspace.GetProjectContextCollection(projectPath)
                           .EnsureValid(projectPath)
                           .FrameworkOnlyContexts;

            contexts = framework == null ?
                       contexts :
                       contexts.Where(c => Equals(c.TargetFramework, framework));

            var rids = string.IsNullOrEmpty(runtime) ?
                       DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers() :
                       new[] { runtime };

            return(contexts.Select(c => Workspace.GetRuntimeContext(c, rids)));
        }
Beispiel #2
0
        public int RunTests(DotnetTestParams dotnetTestParams)
        {
            var projectPath        = GetProjectPath(dotnetTestParams.ProjectOrAssemblyPath);
            var runtimeIdentifiers = !string.IsNullOrEmpty(dotnetTestParams.Runtime)
                ? new[] { dotnetTestParams.Runtime }
                : DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers();
            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);
        }
Beispiel #3
0
        private void CalculateDefaultsForNonAssigned()
        {
            if (string.IsNullOrWhiteSpace(Project))
            {
                Project = Directory.GetCurrentDirectory();
            }

            if (string.IsNullOrWhiteSpace(Configuration))
            {
                Configuration = Constants.DefaultConfiguration;
            }

            var frameworkContexts = _workspace.GetProjectContextCollection(Project)
                                    .EnsureValid(Project)
                                    .FrameworkOnlyContexts;

            var rids = DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers(DotnetFiles.VersionFileObject);

            ProjectContext frameworkContext;

            if (Framework == null)
            {
                if (frameworkContexts.Count() == 1)
                {
                    frameworkContext = frameworkContexts.Single();
                }
                else
                {
                    frameworkContext = frameworkContexts.FirstOrDefault(c => DefaultFrameworks.Contains(c.TargetFramework.Framework));
                }
            }
            else
            {
                frameworkContext = frameworkContexts.FirstOrDefault(c => Equals(c.TargetFramework, NuGetFramework.Parse(Framework)));
            }

            if (frameworkContext == null)
            {
                throw new InvalidOperationException($"Couldn't find target to run. Possible causes:" + Environment.NewLine +
                                                    "1. No project.lock.json file or restore failed - run `dotnet restore`" + Environment.NewLine +
                                                    Framework == null ?
                                                    $"2. project.lock.json has multiple targets none of which is in default list ({string.Join(", ", DefaultFrameworks)})" :
                                                    $"2. The project does not support the desired framework: {Framework}");
            }

            _context = _workspace.GetRuntimeContext(frameworkContext, rids);

            if (Args == null)
            {
                _args = new List <string>();
            }
            else
            {
                _args = new List <string>(Args);
            }
        }
Beispiel #4
0
        public IEnumerable <string> GetRuntimes()
        {
            var rids = new List <string>();

            if (string.IsNullOrEmpty(RuntimeValue))
            {
                return(DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers());
            }
            else
            {
                return(new[] { RuntimeValue });
            }
        }
        public void It_sets_depsfile_in_build_base_path_in_commandspec()
        {
            var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();

            var testInstance = TestAssetsManager.CreateTestInstance(ProjectJsonTestProjectName)
                               .WithLockFiles();

            var buildBasePath = Path.Combine(testInstance.Path, "basedir");

            var commandResolverArguments = new CommandResolverArguments
            {
                CommandName      = "dotnet-hello",
                CommandArguments = null,
                ProjectDirectory = testInstance.Path,
                Configuration    = "Debug",
                Framework        = FrameworkConstants.CommonFrameworks.NetCoreApp10,
                BuildBasePath    = buildBasePath
            };

            var buildCommand = new BuildCommand(
                Path.Combine(testInstance.Path, "project.json"),
                buildBasePath: buildBasePath,
                framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
                               .Execute().Should().Pass();

            var projectContext = ProjectContext.Create(
                testInstance.Path,
                FrameworkConstants.CommonFrameworks.NetCoreApp10,
                DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers(DotnetFiles.VersionFileObject));

            var depsFilePath =
                projectContext.GetOutputPaths("Debug", buildBasePath).RuntimeFiles.DepsJson;

            var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);

            result.Should().NotBeNull();
            result.Args.Should().Contain($"--depsfile {depsFilePath}");
        }