Skip to content

Commit

Permalink
Discover project list for tests and assembly info (NuGet#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen authored Jul 26, 2024
1 parent 0415c7d commit caca1e9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols Condition="'$(IncludeSymbols)' == ''">true</IncludeSymbols>
<SymbolPackageFormat Condition="'$(SymbolPackageFormat)' == ''">snupkg</SymbolPackageFormat>
<LangVersion>9.0</LangVersion>
<LangVersion>latest</LangVersion>

<!--
Our builds generate a file containing the desired assembly version attributes.
Expand Down
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled>
<EnablePackageVersionOverride>false</EnablePackageVersionOverride>

<NuGetClientPackageVersion>6.9.1</NuGetClientPackageVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion NuGet.Server.Common.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{7783A106
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{39C4007F-90EB-49A9-A41E-21861F81869C}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
global.json = global.json
EndProjectSection
EndProject
Expand Down
49 changes: 14 additions & 35 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ if (-not $BuildNumber) {
Trace-Log "Build #$BuildNumber started at $startTime"

$BuildErrors = @()
$CommonSolution = Join-Path $PSScriptRoot "NuGet.Server.Common.sln"
$CommonProjects = Get-SolutionProjects $CommonSolution

Invoke-BuildStep 'Getting private build tools' { Install-PrivateBuildTools } `
-ev +BuildErrors

Invoke-BuildStep 'Cleaning test results' { Clear-Tests } `
-ev +BuildErrors

Invoke-BuildStep 'Installing NuGet.exe' { Install-NuGet } `
-ev +BuildErrors

Expand All @@ -48,54 +47,34 @@ Invoke-BuildStep 'Clearing package cache' { Clear-PackageCache } `
Invoke-BuildStep 'Clearing artifacts' { Clear-Artifacts } `
-ev +BuildErrors

Invoke-BuildStep 'Set version metadata in AssemblyInfo.cs' {
$CommonAssemblyInfo =
"src\NuGet.Services.Build\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Configuration\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Contracts\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Cursor\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.FeatureFlags\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Incidents\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.KeyVault\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Licenses\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Logging\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Messaging.Email\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Messaging\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Owin\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.ServiceBus\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Sql\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Status.Table\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Status\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Storage\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Testing.Entities\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Validation.Issues\Properties\AssemblyInfo.g.cs",
"src\NuGet.Services.Validation\Properties\AssemblyInfo.g.cs"
$CommonAssemblyInfo | ForEach-Object {
Set-VersionInfo (Join-Path $PSScriptRoot $_) -AssemblyVersion $CommonAssemblyVersion -PackageVersion $CommonPackageVersion -Branch $Branch -Commit $CommitSHA
Invoke-BuildStep 'Setting common version metadata in AssemblyInfo.cs' {
$CommonProjects | Where-Object { !$_.IsTest } | ForEach-Object {
$Path = Join-Path $_.Directory "Properties\AssemblyInfo.g.cs"
Set-VersionInfo $Path -AssemblyVersion $CommonAssemblyVersion -PackageVersion $CommonPackageVersion -Branch $Branch -Commit $CommitSHA
}
} `
-ev +BuildErrors

Invoke-BuildStep 'Restoring solution packages' { `
Install-SolutionPackages -path (Join-Path $PSScriptRoot "packages.config") -output (Join-Path $PSScriptRoot "packages") -excludeversion } `
$SolutionPath = Join-Path $PSScriptRoot "packages.config"
$PackagesDir = Join-Path $PSScriptRoot "packages"
Install-SolutionPackages -path $SolutionPath -output $PackagesDir -ExcludeVersion
} `
-skip:$SkipRestore `
-ev +BuildErrors

Invoke-BuildStep 'Removing .editorconfig file in ServerCommon' { Remove-EditorconfigFile -Directory $PSScriptRoot } `
-ev +BuildErrors

Invoke-BuildStep 'Building solution' { `
Invoke-BuildStep 'Building common solution' { `
$SolutionPath = Join-Path $PSScriptRoot "NuGet.Server.Common.sln"
Build-Solution -Configuration $Configuration -BuildNumber $BuildNumber -SolutionPath $SolutionPath -SkipRestore:$SkipRestore
} `
-ev +BuildErrors

Invoke-BuildStep 'Signing the binaries' {
Sign-Binaries -Configuration $Configuration -BuildNumber $BuildNumber `
Sign-Binaries -Configuration $Configuration -BuildNumber $BuildNumber
} `
-ev +BuildErrors

Invoke-BuildStep 'Creating artifacts' { `
Invoke-BuildStep 'Creating common artifacts' { `
$CommonProjects =
"src\NuGet.Services.Build\NuGet.Services.Build.csproj",
"src\NuGet.Services.Configuration\NuGet.Services.Configuration.csproj",
Expand Down Expand Up @@ -124,7 +103,7 @@ Invoke-BuildStep 'Creating artifacts' { `
-ev +BuildErrors

Invoke-BuildStep 'Signing the packages' {
Sign-Packages -Configuration $Configuration -BuildNumber $BuildNumber `
Sign-Packages -Configuration $Configuration -BuildNumber $BuildNumber
} `
-ev +BuildErrors

Expand Down
67 changes: 30 additions & 37 deletions build/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ Function Invoke-BuildStep {
[switch]$SkipExecution
)
if (-not $SkipExecution) {
if ($env:TEAMCITY_VERSION) {
Write-Output "##teamcity[blockOpened name='$BuildStep']"
if ($env:TF_BUILD) {
Write-Output "##[group]$BuildStep"
}

Trace-Log "[BEGIN] $BuildStep"
Expand All @@ -185,8 +185,8 @@ Function Invoke-BuildStep {
}
}

if ($env:TEAMCITY_VERSION) {
Write-Output "##teamcity[blockClosed name='$BuildStep']"
if ($env:TF_BUILD) {
Write-Output "##[endgroup]"
}
}
}
Expand Down Expand Up @@ -416,6 +416,7 @@ Function Update-Submodule {

Invoke-Git -Arguments $args
}

Function Install-NuGet {
[CmdletBinding()]
param()
Expand All @@ -435,7 +436,7 @@ Function Install-NuGet {

Trace-Log 'Downloading nuget.exe'
Invoke-WebRequest `
https://dist.nuget.org/win-x86-commandline/v6.2.1/nuget.exe `
https://dist.nuget.org/win-x86-commandline/v6.10.1/nuget.exe `
-UseBasicParsing `
-OutFile $NuGetExe

Expand Down Expand Up @@ -490,38 +491,6 @@ Function Configure-NuGetCredentials {
}
}

Function Install-DotnetCLI {
[CmdletBinding()]
param()

Trace-Log 'Downloading Dotnet CLI'

New-Item -ItemType Directory -Force -Path $CLIRoot | Out-Null

$env:DOTNET_HOME=$CLIRoot
$env:DOTNET_INSTALL_DIR=$NuGetClientRoot

$installDotnet = Join-Path $CLIRoot "dotnet-install.ps1"

Invoke-WebRequest -UseBasicParsing 'https://raw.githubusercontent.com/dotnet/cli/release/3.1.4xx/scripts/obtain/dotnet-install.ps1' -OutFile $installDotnet

& $installDotnet -Channel 3.1 -i $CLIRoot -Version latest

if (-not (Test-Path $DotNetExe)) {
Error-Log "Unable to find dotnet.exe. The CLI install may have failed." -Fatal
}

# Delete a project template that is unused and is causing a Component Governance issue.
# See https://github.com/dotnet/aspnetcore/issues/20001
$templatesToRemove = Get-ChildItem (Join-Path $CLIRoot "sdk\**\Templates\microsoft.dotnet.web.projecttemplate*")
Trace-Log "Removing items: "
$templatesToRemove
$templatesToRemove | Remove-Item -Force -Recurse

# Display build info
& $DotNetExe --info
}

Function Get-BuildNumber() {
$SemanticVersionDate = '2016-06-22'
[int](((Get-Date) - (Get-Date $SemanticVersionDate)).TotalMinutes / 5)
Expand Down Expand Up @@ -620,6 +589,30 @@ Function Restore-SolutionPackages {
}
}

function Get-SolutionProjects($SolutionPath) {
$paths = dotnet sln $SolutionPath list | Where-Object { $_ -like "*.csproj" }
if (!$paths) {
throw "Failed to find .csproj files found in solution $SolutionPath."
}

$solutionDir = Split-Path (Resolve-Path $SolutionPath)

$projects = $paths | ForEach-Object {
$projectPath = Join-Path $solutionDir $_
$projectRelativeDir = Split-Path $_
$projectDir = Join-Path $solutionDir $projectRelativeDir
$isTestProject = $projectRelativeDir -like "test*";
return [PSCustomObject]@{
IsTest = $isTestProject;
Directory = $projectDir;
Path = $projectPath;
RelativePath = $_;
}
}

return $projects | Sort-Object -Property Path
}

Function Get-PackageVersion() {
[CmdletBinding()]
param(
Expand Down
61 changes: 21 additions & 40 deletions test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,8 @@ trap {
exit 1
}

$CLIRoot = $PSScriptRoot
$env:DOTNET_INSTALL_DIR = $CLIRoot

. "$PSScriptRoot\build\common.ps1"

Function Invoke-Tests {
[CmdletBinding()]
param()

Trace-Log 'Running tests'

$xUnitExe = (Join-Path $PSScriptRoot "packages\xunit.runner.console\tools\net472\xunit.console.exe")

$CommonTestAssemblies =
"tests\NuGet.Services.Configuration.Tests\bin\$Configuration\net472\NuGet.Services.Configuration.Tests.dll",
"tests\NuGet.Services.Contracts.Tests\bin\$Configuration\net472\NuGet.Services.Contracts.Tests.dll",
"tests\NuGet.Services.Cursor.Tests\bin\$Configuration\net472\NuGet.Services.Cursor.Tests.dll",
"tests\NuGet.Services.KeyVault.Tests\bin\$Configuration\net472\NuGet.Services.KeyVault.Tests.dll",
"tests\NuGet.Services.Licenses.Tests\bin\$Configuration\net472\NuGet.Services.Licenses.Tests.dll",
"tests\NuGet.Services.Logging.Tests\bin\$Configuration\net472\NuGet.Services.Logging.Tests.dll",
"tests\NuGet.Services.Messaging.Email.Tests\bin\$Configuration\net472\NuGet.Services.Messaging.Email.Tests.dll",
"tests\NuGet.Services.Messaging.Tests\bin\$Configuration\net472\NuGet.Services.Messaging.Tests.dll",
"tests\NuGet.Services.Owin.Tests\bin\$Configuration\net472\NuGet.Services.Owin.Tests.dll",
"tests\NuGet.Services.Sql.Tests\bin\$Configuration\net472\NuGet.Services.Sql.Tests.dll",
"tests\NuGet.Services.Status.Tests\bin\$Configuration\net472\NuGet.Services.Status.Tests.dll",
"tests\NuGet.Services.Validation.Issues.Tests\bin\$Configuration\net472\NuGet.Services.Validation.Issues.Tests.dll",
"tests\NuGet.Services.Validation.Tests\bin\$Configuration\net472\NuGet.Services.Validation.Tests.dll"

$TestCount = 0

$CommonTestAssemblies | ForEach-Object {
$TestResultFile = Join-Path $PSScriptRoot "Results.$TestCount.xml"
& $xUnitExe (Join-Path $PSScriptRoot $_) -xml $TestResultFile
if (-not (Test-Path $TestResultFile)) {
Write-Error "The test run failed to produce a result file";
exit 1;
}
$TestCount++
}
}

Write-Host ("`r`n" * 3)
Trace-Log ('=' * 60)

Expand All @@ -64,8 +25,28 @@ if (-not $BuildNumber) {
Trace-Log "Build #$BuildNumber started at $startTime"

$TestErrors = @()
$CommonSolution = Join-Path $PSScriptRoot "NuGet.Server.Common.sln"
$CommonProjects = Get-SolutionProjects $CommonSolution

Invoke-BuildStep 'Running tests' { Invoke-Tests } `
Invoke-BuildStep 'Cleaning test results' { Clear-Tests } `
-ev +BuildErrors

Invoke-BuildStep 'Running tests' {
$CommonTestProjects = $CommonProjects | Where-Object { $_.IsTest }

$TestCount = 0

$CommonTestProjects | ForEach-Object {
$TestResultFile = Join-Path $PSScriptRoot "Results.$TestCount.xml"
Trace-Log "Testing $($_.Path)"
dotnet test $_.Path --no-restore --no-build --configuration $Configuration "-l:trx;LogFileName=$TestResultFile"
if (-not (Test-Path $TestResultFile)) {
Write-Error "The test run failed to produce a result file";
exit 1;
}
$TestCount++
}
} `
-ev +TestErrors

Trace-Log ('-' * 60)
Expand Down
11 changes: 11 additions & 0 deletions tests/NuGet.Services.Contracts.Tests/ContractsFacts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using NuGet.Services.Validation;
using Xunit;

Expand All @@ -13,6 +14,12 @@ public void ShouldOnlyHaveInterfacesAndEnums()
{
// Arrange
var assembly = typeof(ValidationStatus).Assembly;
var exclude = new[]
{
// included in the assembly by newer language versions
"Microsoft.CodeAnalysis.EmbeddedAttribute",
"System.Runtime.CompilerServices.RefSafetyRulesAttribute",
};

// Act
var types = assembly.GetTypes();
Expand All @@ -21,6 +28,10 @@ public void ShouldOnlyHaveInterfacesAndEnums()
Assert.NotEmpty(types);
foreach (var type in types)
{
if (exclude.Contains(type.FullName))
{
continue;
}
Assert.True(type.IsEnum || type.IsInterface, $"{type.FullName} must either be an interface or an enum.");
}
}
Expand Down

0 comments on commit caca1e9

Please sign in to comment.