Skip to content

Commit a1cd0cf

Browse files
Added script for running verification test locally (#295)
1 parent 4335896 commit a1cd0cf

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

docs/running-verification-tests.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
# Running Verification Tests On Your Local Machine
22
Verification tests are used to confirm that no detectors are lost when changes are made to the project. The tests are run on every PR build. They work by comparing the detection results from the main branch to detection results from the new changes on your PR. You can follow the steps below to run them locally.
33

4-
## Step 1 : Run Detection on the main branch
4+
An automation script is created to setup the test artifact and execute the verification test. Ensure that your local changes builds and execute following powershell script **from repository root**.
5+
6+
```
7+
.\test\Microsoft.ComponentDetection.VerificationTests\resources\VerificationTest.ps1
8+
```
9+
10+
### Debug using visual studio
11+
The automation script above will output variables that needs to be replaced in `ComponentDetectionIntegrationTests.cs` file. Then, verification test can be debugged through Test Explorer by opening `\test\Microsoft.ComponentDetection.VerificationTests\Microsoft.DependencyDetective.VerificationTests.csproj` in visual studio.
12+
13+
14+
## Manual setup
15+
16+
### Step 1 : Run Detection on the main branch
517

618
- Checkout the main branch in your local repo
719
- Create a folder to store detection results (e.g C:\old-output-folder)
@@ -14,7 +26,7 @@ For Example:
1426
```dotnet run scan --Verbosity Verbose --SourceDirectory C:\componentdetection --Output C:\old-output-folder```
1527

1628

17-
## Step 2 : Run Detection on your new branch
29+
### Step 2 : Run Detection on your new branch
1830

1931
- Checkout the branch with the new changes you are trying to merge
2032
- Create a folder to store detection results. This folder should be seperate from the one you used in Step 1 (e.g C:\new-output-folder)
@@ -26,7 +38,7 @@ For Example:
2638

2739
```dotnet run scan --Verbosity Verbose --SourceDirectory C:\componentdetection --Output C:\new-output-folder```
2840

29-
## Step 3 : Update variables in the test
41+
### Step 3 : Update variables in the test
3042

3143
- Open the Microsoft.ComponentDetection.VerificationTests project in VS Studio
3244
- Navigate to `GatherResources()` in `ComponentDetectionIntegrationTests.cs`
@@ -36,7 +48,7 @@ For Example:
3648
- `allowedTimeDriftRatioString`: This should be ".75"
3749

3850

39-
## Step 4: Run The tests
51+
### Step 4: Run The tests
4052
You can run the tests in two ways:
4153
- Run or Debug the tests in test explorer.
4254
- Use the command line to navigate to the Microsoft.ComponentDetection.VerificationTests folder, and run `dotnet test`.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Set-StrictMode -Version 2.0
2+
$Script:ErrorActionPreference = 'Stop'
3+
4+
function main()
5+
{
6+
Write-Progress "starting verification tests."
7+
$repoPath = Get-Location
8+
$workspace = (Get-Item $repoPath).Parent.FullName
9+
10+
# Test data directory is cleaned up before each run, we want to avoid the conflict and accidental removal of any other existing directory.
11+
# incorporating uid in testdata directory ensures there is no conflict in directory name.
12+
$uidForTestData = "1f8835c2"
13+
$testDataDir = $workspace + "\cd-verification-testdata-" + $uidForTestData
14+
15+
if (Test-Path $testDataDir) {
16+
Write-Progress "Removing existing test data directory from previous runs at $testDataDir"
17+
Remove-Item $testDataDir -Force -Recurse
18+
}
19+
20+
Write-Progress "Creating test data directory at $testDataDir"
21+
mkdir $testDataDir
22+
23+
$verificationTestRepo = (Get-Item $repoPath).FullName + "\test\Microsoft.ComponentDetection.VerificationTests\resources"
24+
$CDRelease = $testDataDir + "\component-detection-release"
25+
$output = $testDataDir + "\output"
26+
$releaseOutput = $testDataDir + "\release-output"
27+
28+
Write-Progress "cloning released component-detection at $CDRelease"
29+
git clone "https://github.com/microsoft/component-detection.git" $CDRelease
30+
31+
mkdir $output
32+
mkdir $releaseOutput
33+
34+
Write-Progress "Running detection....."
35+
Set-Location (Get-Item $repoPath).FullName
36+
dotnet restore
37+
Set-Location ((Get-Item $repoPath).FullName + "\src\Microsoft.ComponentDetection")
38+
dotnet run scan --SourceDirectory $verificationTestRepo --Output $output
39+
40+
Set-Location $CDRelease
41+
dotnet restore
42+
Set-Location ($CDRelease + "\src\Microsoft.ComponentDetection")
43+
dotnet run scan --SourceDirectory $verificationTestRepo --Output $releaseOutput
44+
45+
46+
47+
$env:GITHUB_OLD_ARTIFACTS_DIR = $releaseOutput
48+
$env:GITHUB_NEW_ARTIFACTS_DIR = $output
49+
$env:ALLOWED_TIME_DRIFT_RATIO = "0.75"
50+
51+
Write-Progress "Executing verification tests....."
52+
Set-Location ((Get-Item $repoPath).FullName + "\test\Microsoft.ComponentDetection.VerificationTests\")
53+
dotnet restore
54+
dotnet test
55+
56+
Set-Location $repoPath
57+
58+
Write-Host "Verification tests were completed. The generated testdata can be found at $testDataDir `n To debug test with visual studio, replace values for following variables in ComponentDetectionIntegrationTests.cs" -ForegroundColor red -BackgroundColor white
59+
Write-Host "oldGithubArtifactsDir = @`"$releaseOutput`"" -ForegroundColor red -BackgroundColor white
60+
Write-Host "newGithubArtifactsDir = @`"$output`"" -ForegroundColor red -BackgroundColor white
61+
Write-Host "allowedTimeDriftRatioString = "`"0.75`" -ForegroundColor red -BackgroundColor white
62+
}
63+
64+
main

0 commit comments

Comments
 (0)