forked from exercism/csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ps1
129 lines (105 loc) · 4.17 KB
/
test.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<#
.SYNOPSIS
Test the solution.
.DESCRIPTION
Test the solution to verify correctness. This script verifies that:
- The config.json file is correct
- The generators project builds successfully
- The example implementations pass the test suites
- The refactoring projects stub files pass the test suites
.PARAMETER Exercise
The slug of the exercise to verify (optional).
.EXAMPLE
The example below will verify the full solution
PS C:\> ./test.ps1
.EXAMPLE
The example below will verify the "acronym" exercise
PS C:\> ./test.ps1 acronym
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Position = 0, Mandatory = $false)]
[string]$Exercise
)
# Import shared functionality
. ./shared.ps1
function Invoke-Configlet-Lint {
Write-Output "Linting config.json"
Invoke-CallScriptExitOnError { ./bin/fetch-configlet }
Invoke-CallScriptExitOnError { ./bin/configlet lint }
}
function Invoke-Build-Generators {
Write-Output "Building generators"
Invoke-CallScriptExitOnError { dotnet build ./generators }
}
function Clean($BuildDir) {
Write-Output "Cleaning previous build"
Remove-Item -Recurse -Force $BuildDir -ErrorAction Ignore
}
function Copy-Exercise($SourceDir, $BuildDir) {
Write-Output "Copying exercises"
Copy-Item $SourceDir -Destination $BuildDir -Recurse
}
function Enable-All-UnitTests($BuildDir) {
Write-Output "Enabling all tests"
Get-ChildItem -Path $BuildDir -Include "*Tests.cs" -Recurse | ForEach-Object {
(Get-Content $_.FullName) -replace "Skip = ""Remove this Skip property to run this test""", "" | Set-Content $_.FullName
}
}
function Test-Refactoring-Projects($PracticeExercisesDir) {
Write-Output "Testing refactoring projects"
@("tree-building", "ledger", "markdown") | ForEach-Object {
Invoke-CallScriptExitOnError { dotnet test "$practiceExercisesDir/$_" }
}
}
function Set-ExampleImplementation {
[CmdletBinding(SupportsShouldProcess)]
param($ExercisesDir, $ReplaceFileName)
if ($PSCmdlet.ShouldProcess("Exercise $ReplaceFileName", "replace solution with example")) {
Get-ChildItem -Path $ExercisesDir -Include "*.csproj" -Recurse | ForEach-Object {
$stub = Join-Path -Path $_.Directory ($_.BaseName + ".cs")
$example = Join-Path -Path $_.Directory ".meta" $ReplaceFileName
Move-Item -Path $example -Destination $stub -Force
}
}
}
function Use-ExampleImplementation {
[CmdletBinding(SupportsShouldProcess)]
param($ConceptExercisesDir, $PracticeExercisesDir)
if ($PSCmdlet.ShouldProcess("Exercises directory", "replace all solutions with corresponding examples")) {
Write-Output "Replacing concept exercise stubs with exemplar"
Set-ExampleImplementation $ConceptExercisesDir "Exemplar.cs"
Write-Output "Replacing practice exercise stubs with example"
Set-ExampleImplementation $PracticeExercisesDir "Example.cs"
}
}
function Test-ExerciseImplementation($Exercise, $BuildDir, $ConceptExercisesDir, $PracticeExercisesDir) {
Write-Output "Running tests"
if (-Not $Exercise) {
Invoke-CallScriptExitOnError { dotnet test "$BuildDir/Exercises.sln" }
}
elseif (Test-Path "$ConceptExercisesDir/$Exercise") {
Invoke-CallScriptExitOnError { dotnet test "$ConceptExercisesDir/$Exercise" }
}
elseif (Test-Path "$PracticeExercisesDir/$Exercise") {
Invoke-CallScriptExitOnError { dotnet test "$PracticeExercisesDir/$Exercise" }
}
else {
throw "Could not find exercise '$Exercise'"
}
}
$buildDir = Join-Path $PSScriptRoot "build"
$practiceExercisesDir = Join-Path $buildDir "practice"
$conceptExercisesDir = Join-Path $buildDir "concept"
$sourceDir = Resolve-Path "exercises"
Invoke-Configlet-Lint
Clean $buildDir
Copy-Exercise $sourceDir $buildDir
Enable-All-UnitTests $buildDir
if (!$Exercise) {
Invoke-Build-Generators
Test-Refactoring-Projects $practiceExercisesDir
}
Use-ExampleImplementation $conceptExercisesDir $practiceExercisesDir
Test-ExerciseImplementation -Exercise $Exercise -BuildDir $buildDir -ConceptExercisesDir $conceptExercisesDir -PracticeExercisesDir $practiceExercisesDir
exit $LastExitCode