Skip to content

Commit a4e2ac2

Browse files
committed
Miscellaneous
remove user-agents.dat filter - workflow run + it has to be versioned update-data - push/pop-location safety added compatible ps editions updated tests - conversion test
1 parent 71fcf57 commit a4e2ac2

9 files changed

+79
-71
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Fake-UserAgent/data/user-agents.dat
21
temp/

Fake-UserAgent/Fake-UserAgent.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Copyright = '(c) Alan Plocieniak. All rights reserved.'
99
Description = 'PowerShell module for generating random User Agents.'
1010
PowerShellVersion = '5.0'
11+
CompatiblePSEditions = 'Desktop', 'Core'
1112
FunctionsToExport = '*'
1213
PrivateData = @{
1314
PSData = @{

Fake-UserAgent/Private/Get-UserAgentData.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function Get-UserAgentData {
44
$datPath = Join-Path -Path $PSScriptRoot -ChildPath "..\data\user-agents.dat"
55

66
if (-Not(Test-Path $datPath)) {
7-
Initialize-Dataset
7+
Write-Error 'User agent data not found.'
88
}
99

1010
Import-Clixml -Path $datPath

Fake-UserAgent/Public/Get-UserAgent.ps1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ function Get-UserAgent {
33
param ()
44

55
$data = Get-UserAgentData
6-
return $data
6+
return ,$data
77

8-
}
9-
10-
Export-ModuleMember -Function Get-UserAgent
8+
}

Fake-UserAgent/data/user-agents.dat

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

Tests/Fake-UserAgent.Tests.ps1

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Import-Module .\Fake-UserAgent\Fake-UserAgent.psm1 -Force
2+
3+
Describe 'Fake-UserAgent.Tests' {
4+
Context "Get-UserAgentData Tests" {
5+
6+
BeforeAll {
7+
$datPath = '.\Fake-UserAgent\data\user-agents.dat'
8+
}
9+
10+
It "should have initialized state" {
11+
Test-Path $datPath | Should -Be $true
12+
}
13+
14+
It "should have user agents" {
15+
$agents = Get-UserAgent
16+
$agents | Should -Not -BeNullOrEmpty
17+
$agents.Count | Should -BeGreaterThan 0
18+
}
19+
}
20+
21+
Context "UA file update Tests" {
22+
23+
BeforeAll {
24+
$datPath = '.\Fake-UserAgent\data\user-agents.dat'
25+
Remove-Item -Path $datPath -ErrorAction SilentlyContinue
26+
}
27+
28+
It "should have broken state" {
29+
Test-Path $datPath | Should -Be $false
30+
{ Get-UserAgent -ErrorAction SilentlyContinue } | Should -Throw
31+
}
32+
33+
It "should download and update user agents" {
34+
{ .\update-data.ps1 6>$null } | Should -Not -Throw
35+
}
36+
37+
It "should have initialized state" {
38+
Test-Path $datPath | Should -Be $true
39+
}
40+
41+
It "should have the same number of user agents" {
42+
$raw = Get-Content .\temp\user-agents.json -Raw | ConvertFrom-Json
43+
$processed = Get-UserAgent
44+
$processed.Count | Should -Be $raw.Count
45+
}
46+
47+
It "should have the same user agent values" {
48+
$raw = Get-Content .\temp\user-agents.json -Raw | ConvertFrom-Json
49+
$processed = Get-UserAgent
50+
51+
for ($i = 0; $i -lt 200; $i++) {
52+
$processed[$i].ua | Should -Be $raw[$i].userAgent
53+
$processed[$i].type | Should -Be $raw[$i].deviceCategory
54+
$processed[$i].platform | Should -Be $raw[$i].platform
55+
}
56+
}
57+
}
58+
}

Tests/Toggl.API.Tests.ps1

Lines changed: 0 additions & 55 deletions
This file was deleted.

scripts/Initialize-Dataset.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ function Initialize-Dataset {
44
)
55
Process {
66

7-
Get-Content $uaJsonPath -Raw | ConvertFrom-Json | % {
7+
$uaParser = [UAParser.Parser]::GetDefault()
8+
9+
[System.Object[]]$uas = Get-Content $uaJsonPath -Raw | ConvertFrom-Json
10+
$uas | % {
811
$ua_result = $uaParser.Parse($_.userAgent)
912
[PSCustomObject]@{
1013
ua = $_.userAgent

update-data.ps1

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ if (Test-Path $temp) {
1212
Remove-Item -Path $temp -Recurse -Force
1313
}
1414
New-Item -ItemType Directory -Path $temp | Out-Null
15-
Push-Location -Path $temp
1615

1716

18-
Write-Host 'Downloading user-agents.json...'
19-
Invoke-WebRequest -Uri $uaJsonUri -OutFile "user-agents.json.gz"
17+
try {
18+
Push-Location -Path $temp
19+
Write-Host 'Downloading user-agents.json...'
20+
Invoke-WebRequest -Uri $uaJsonUri -OutFile "user-agents.json.gz"
2021

21-
Write-Host 'Decompressing user-agents.json...'
22-
ConvertFrom-Gzip -InputObject (Get-Item "user-agents.json.gz") -RemoveInputFile
22+
Write-Host 'Decompressing user-agents.json...'
23+
ConvertFrom-Gzip -InputObject (Get-Item "user-agents.json.gz") -RemoveInputFile
2324

24-
Write-Host 'Processing user-agents.json...'
25-
$processed = Initialize-Dataset -uaJsonPath "user-agents.json"
26-
27-
Pop-Location
25+
Write-Host 'Processing user-agents.json...'
26+
$processed = Initialize-Dataset -uaJsonPath "user-agents.json"
27+
}
28+
finally {
29+
Pop-Location
30+
}
2831

2932
Write-Host 'Exporting user-agents.dat...'
3033
Push-Location .\Fake-UserAgent\data

0 commit comments

Comments
 (0)