-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathGet-TestOrQueryDirectoryForCurrentFile.ps1
More file actions
148 lines (109 loc) · 4.75 KB
/
Get-TestOrQueryDirectoryForCurrentFile.ps1
File metadata and controls
148 lines (109 loc) · 4.75 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env pwsh
param(
[Parameter(Mandatory)]
[string]
$CurrentFile
)
<#
We find the opposite of whatever they pass in. If the user gives us the query
directory, we give them the test directory and if they give us the test
directory, we give them the query directory.
Note that this works for shared queries as well.
#>
# Logic
# 1) Shared CPP queries get redirected to the Shared CPP Test
# 2) Shared CPP Tests get redirected to the Shared Query
# 3) Shared C Tests get redirected to the shared CPP Query
# 4) Shared C Queries Don't exist.
# 5) CPP Queries get redirected to the CPP Test
# 6) CPP Tests get redirected to the CPP Query
# 7) C Queries get redirected to C tests
# 8) C Tests get redirected to C queries
# (1) Shared CPP Query
#$CurrentFile = "C:\Projects\codeql-coding-standards\cpp\common\src\codingstandards\cpp\rules\sectionsofcodeshallnotbecommentedout\SectionsOfCodeShallNotBeCommentedOut.qll"
# (2) Shared CPP Test
#$CurrentFile = "C:\Projects\codeql-coding-standards\cpp\common\test\rules\catchblockshadowing\CatchBlockShadowing.ql"
# (3) Shared C Test (***Not WOrking)
#$CurrentFile = "C:\Projects\codeql-coding-standards\c\common\test\rules\donotaccessaclosedfile\DoNotAccessAClosedFile.ql"
# (4) Shared C Queries (DNE)
# (5) CPP Query
#$CurrentFile = "C:\Projects\codeql-coding-standards\cpp\autosar\src\rules\A2-7-2\SectionsOfCodeCommentedOut.ql"
# (6) CPP Test
#$CurrentFile = "C:\Projects\codeql-coding-standards\cpp\autosar\test\rules\A2-7-2\SectionsOfCodeCommentedOut.testref"
# (7) C Query
#$CurrentFile = "C:\Projects\codeql-coding-standards\c\cert\src\rules\FIO42-C\CloseFilesWhenTheyAreNoLongerNeeded.ql"
# (8) C Test
#$CurrentFile = "C:\Projects\codeql-coding-standards\c\cert\test\rules\FIO42-C\CloseFilesWhenTheyAreNoLongerNeeded.qlref"
$CurrentFile = Resolve-Path $CurrentFile # convert this to a real thing
$CPPSharedTest = $false
$CSharedTest = $false
# it's a cpp shared query
if(($CurrentFile.toString().IndexOf("\cpp\common\") -ge 0) -and ($CurrentFile.toString().IndexOf("\cpp\rules\") -ge 0)){
Write-Host "Detected Shared CPP Query..."
$OneAbove = Resolve-Path(Join-Path $CurrentFile ..\..\..\..\..\..\)
$CurrentPathSrcOrTest = Resolve-Path(Join-Path $CurrentFile ..\..\..\..\..\)
$CurrentRule = Resolve-Path(Join-Path $CurrentFile ..\)
$Stem = Split-Path -Path $CurrentPathSrcOrTest -Leaf
$Rule = Split-Path -Path $CurrentRule -Leaf
}
# shared cpp test \src\codingstandards\cpp\rules
elseif(($CurrentFile.toString().IndexOf("\cpp\common\test\rules") -ge 0)){
Write-Host "Detected Shared CPP Test..."
$OneAbove = Resolve-Path(Join-Path $CurrentFile ..\..\..\..\)
$CurrentPathSrcOrTest = Resolve-Path(Join-Path $CurrentFile ..\..\..\)
$CurrentRule = Resolve-Path(Join-Path $CurrentFile ..\)
$Stem = Split-Path -Path $CurrentPathSrcOrTest -Leaf
$Rule = Split-Path -Path $CurrentRule -Leaf
$CPPSharedTest = $true
}
# it's a c shared test
elseif(($CurrentFile.toString().IndexOf("\c\common\test\rules\") -ge 0)){
Write-Host "Detected Shared C Test..."
$OneAbove = Resolve-Path(Join-Path $CurrentFile ..\..\..\..\..\..\)
$CurrentPathSrcOrTest = Resolve-Path(Join-Path $CurrentFile ..\..\..\)
$CurrentRule = Resolve-Path(Join-Path $CurrentFile ..\)
$Stem = Split-Path -Path $CurrentPathSrcOrTest -Leaf
$Rule = Split-Path -Path $CurrentRule -Leaf
$CSharedTest = $true
}
# it's a normal query
else{
Write-Host "Detected Non-Shared Query..."
$OneAbove = Resolve-Path(Join-Path $CurrentFile ..\..\..\..\)
$CurrentPathSrcOrTest = Resolve-Path(Join-Path $CurrentFile ..\..\..\)
$CurrentRule = Resolve-Path(Join-Path $CurrentFile ..\)
$Stem = Split-Path -Path $CurrentPathSrcOrTest -Leaf
$Rule = Split-Path -Path $CurrentRule -Leaf
}
Write-Host "Input Path : $($CurrentFile)"
Write-Host "CurrentSrcOrTest Path: $($CurrentPathSrcOrTest)"
Write-Host "One Above : $($OneAbove)"
Write-Host "Stem : $($Stem)"
Write-Host "Rule : $($Rule)"
if($Stem -eq "test"){
$SwitchTo = "src"
# Small hack for inconsistent pathing
if($CPPSharedTest){
$SwitchTo = "\src\codingstandards\cpp"
}
if($CSharedTest){
$SwitchTo = "cpp\common\src\codingstandards\cpp\"
}
}elseif($Stem -eq "src"){
$SwitchTo = "test"
}else{
Write-Host "Not a query file. Exiting."
Exit
}
Write-Host "Switch To : $($SwitchTo)"
$NewPath = (Join-Path (Join-Path (Join-Path $OneAbove $SwitchTo) "rules") $Rule)
Write-Host "Switching to Path: $($NewPath)"
# prefer ql files
$F = (Get-ChildItem "$($NewPath)\*.ql*")
if($F.Length -eq 0){
$Target = (Get-ChildItem "$($NewPath)\*")[0]
}else{
$Target = $F[0]
}
Write-Host "Target File $($Target.FullName)"
code $Target.FullName