Browse code

PowerShell: move $null to left-hand for comparisons

see https://rencore.com/blog/powershell-null-comparison/

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2018/12/23 04:40:36
Showing 1 changed files
... ...
@@ -125,7 +125,7 @@ Function Nuke-Everything {
125 125
 
126 126
     try {
127 127
 
128
-        if ($env:SKIP_ALL_CLEANUP -eq $null) {
128
+        if ($null -eq $env:SKIP_ALL_CLEANUP) {
129 129
             Write-Host -ForegroundColor green "INFO: Nuke-Everything..."
130 130
             $containerCount = ($(docker ps -aq | Measure-Object -line).Lines) 
131 131
             if (-not $LastExitCode -eq 0) {
... ...
@@ -160,11 +160,11 @@ Function Nuke-Everything {
160 160
             Stop-Process -Id $p -Force -ErrorAction SilentlyContinue
161 161
         }
162 162
 
163
-        if ($pidFile -ne $Null) {
163
+        if ($null -ne $pidFile) {
164 164
             Write-Host "INFO: Tidying pidfile $pidfile"
165 165
              if (Test-Path $pidFile) {
166 166
                 $p=Get-Content $pidFile -raw
167
-                if ($p -ne $null){
167
+                if ($null -ne $p){
168 168
                     Write-Host -ForegroundColor green "INFO: Stopping possible daemon pid $p"
169 169
                     taskkill -f -t -pid $p
170 170
                 }
... ...
@@ -190,7 +190,7 @@ Function Nuke-Everything {
190 190
 
191 191
         # Delete the directory using our dangerous utility unless told not to
192 192
         if (Test-Path "$env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR") {
193
-            if (($env:SKIP_ZAP_DUT -ne $null) -or ($env:SKIP_ALL_CLEANUP -eq $null)) {
193
+            if (($null -ne $env:SKIP_ZAP_DUT) -or ($null -eq $env:SKIP_ALL_CLEANUP)) {
194 194
                 Write-Host -ForegroundColor Green "INFO: Nuking $env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR"
195 195
                 docker-ci-zap "-folder=$env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR"
196 196
             } else {
... ...
@@ -256,25 +256,25 @@ Try {
256 256
     Get-ChildItem Env: | Out-String
257 257
 
258 258
     # PR
259
-    if (-not ($env:PR -eq $Null)) { echo "INFO: PR#$env:PR (https://github.com/docker/docker/pull/$env:PR)" }
259
+    if (-not ($null -eq $env:PR)) { echo "INFO: PR#$env:PR (https://github.com/docker/docker/pull/$env:PR)" }
260 260
 
261 261
     # Make sure docker is installed
262
-    if ((Get-Command "docker" -ErrorAction SilentlyContinue) -eq $null) { Throw "ERROR: docker is not installed or not found on path" }
262
+    if ($null -eq (Get-Command "docker" -ErrorAction SilentlyContinue)) { Throw "ERROR: docker is not installed or not found on path" }
263 263
 
264 264
     # Make sure docker-ci-zap is installed
265
-    if ((Get-Command "docker-ci-zap" -ErrorAction SilentlyContinue) -eq $null) { Throw "ERROR: docker-ci-zap is not installed or not found on path" }
265
+    if ($null -eq (Get-Command "docker-ci-zap" -ErrorAction SilentlyContinue)) { Throw "ERROR: docker-ci-zap is not installed or not found on path" }
266 266
 
267 267
     # Make sure SOURCES_DRIVE is set
268
-    if ($env:SOURCES_DRIVE -eq $Null) { Throw "ERROR: Environment variable SOURCES_DRIVE is not set" }
268
+    if ($null -eq $env:SOURCES_DRIVE) { Throw "ERROR: Environment variable SOURCES_DRIVE is not set" }
269 269
 
270 270
     # Make sure TESTRUN_DRIVE is set
271
-    if ($env:TESTRUN_DRIVE -eq $Null) { Throw "ERROR: Environment variable TESTRUN_DRIVE is not set" }
271
+    if ($null -eq $env:TESTRUN_DRIVE) { Throw "ERROR: Environment variable TESTRUN_DRIVE is not set" }
272 272
 
273 273
     # Make sure SOURCES_SUBDIR is set
274
-    if ($env:SOURCES_SUBDIR -eq $Null) { Throw "ERROR: Environment variable SOURCES_SUBDIR is not set" }
274
+    if ($null -eq $env:SOURCES_SUBDIR) { Throw "ERROR: Environment variable SOURCES_SUBDIR is not set" }
275 275
 
276 276
     # Make sure TESTRUN_SUBDIR is set
277
-    if ($env:TESTRUN_SUBDIR -eq $Null) { Throw "ERROR: Environment variable TESTRUN_SUBDIR is not set" }
277
+    if ($null -eq $env:TESTRUN_SUBDIR) { Throw "ERROR: Environment variable TESTRUN_SUBDIR is not set" }
278 278
 
279 279
     # SOURCES_DRIVE\SOURCES_SUBDIR must be a directory and exist
280 280
     if (-not (Test-Path -PathType Container "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR")) { Throw "ERROR: $env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR must be an existing directory" }
... ...
@@ -420,7 +420,7 @@ Try {
420 420
     New-Item -ItemType Directory "$env:TEMP\localappdata" -ErrorAction SilentlyContinue | Out-Null
421 421
     New-Item -ItemType Directory "$env:TEMP\binary" -ErrorAction SilentlyContinue | Out-Null
422 422
     New-Item -ItemType Directory "$env:TEMP\installer" -ErrorAction SilentlyContinue | Out-Null
423
-    if ($env:SKIP_COPY_GO -eq $null) {
423
+    if ($null -eq $env:SKIP_COPY_GO) {
424 424
         # Wipe the previous version of GO - we're going to get it out of the image
425 425
         if (Test-Path "$env:TEMP\go") { Remove-Item "$env:TEMP\go" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null }
426 426
         New-Item -ItemType Directory "$env:TEMP\go" -ErrorAction SilentlyContinue | Out-Null
... ...
@@ -435,15 +435,15 @@ Try {
435 435
     # As of go 1.11, Dockerfile changed to be in the format like "FROM golang:1.11.0 AS base".
436 436
     # If a version number ends with .0 (as in 1.11.0, a convention used in golang docker
437 437
     # image versions), it needs to be removed (i.e. "1.11.0" becomes "1.11").
438
-    if ($goVersionDockerfile -eq $Null) {
438
+    if ($null -eq $goVersionDockerfile) {
439 439
         $goVersionDockerfile=$(Get-Content ".\Dockerfile" | Select-String "^FROM golang:")
440
-        if ($goVersionDockerfile -ne $Null) {
440
+        if ($null -ne $goVersionDockerfile) {
441 441
             $goVersionDockerfile = $goVersionDockerfile.ToString().Split(" ")[1].Split(":")[1] -replace '\.0$',''
442 442
         }
443 443
     } else {
444 444
         $goVersionDockerfile = $goVersionDockerfile.ToString().Split(" ")[2]
445 445
     }
446
-    if ($goVersionDockerfile -eq $Null) {
446
+    if ($null -eq $goVersionDockerfile) {
447 447
         Throw "ERROR: Failed to extract golang version from Dockerfile"
448 448
     }
449 449
     Write-Host  -ForegroundColor Green "INFO: Validating GOLang consistency in Dockerfile.windows..."
... ...
@@ -452,7 +452,7 @@ Try {
452 452
     }
453 453
 
454 454
     # Build the image
455
-    if ($env:SKIP_IMAGE_BUILD -eq $null) {
455
+    if ($null -eq $env:SKIP_IMAGE_BUILD) {
456 456
         Write-Host  -ForegroundColor Cyan "`n`nINFO: Building the image from Dockerfile.windows at $(Get-Date)..."
457 457
         Write-Host
458 458
         $ErrorActionPreference = "SilentlyContinue"
... ...
@@ -478,7 +478,7 @@ Try {
478 478
     }
479 479
 
480 480
     # Build the binary in a container unless asked to skip it.
481
-    if ($env:SKIP_BINARY_BUILD -eq $null) {
481
+    if ($null -eq $env:SKIP_BINARY_BUILD) {
482 482
         Write-Host  -ForegroundColor Cyan "`n`nINFO: Building the test binaries at $(Get-Date)..."
483 483
         $ErrorActionPreference = "SilentlyContinue"
484 484
         docker rm -f $COMMITHASH 2>&1 | Out-Null
... ...
@@ -530,7 +530,7 @@ Try {
530 530
 
531 531
     # Grab the golang installer out of the built image. That way, we know we are consistent once extracted and paths set,
532 532
     # so there's no need to re-deploy on account of an upgrade to the version of GO being used in docker.
533
-    if ($env:SKIP_COPY_GO -eq $null) {
533
+    if ($null -eq $env:SKIP_COPY_GO) {
534 534
         Write-Host -ForegroundColor Green "INFO: Copying the golang package from the container to $env:TEMP\installer\go.zip..."
535 535
         docker cp "$COMMITHASH`:c`:\go.zip" $env:TEMP\installer\
536 536
         if (-not($LastExitCode -eq 0)) {
... ...
@@ -593,7 +593,7 @@ Try {
593 593
     New-Item -ItemType Directory $env:TEMP\daemon -ErrorAction SilentlyContinue  | Out-Null
594 594
 
595 595
     # In LCOW mode, for now we need to set an environment variable before starting the daemon under test
596
-    if (($env:LCOW_MODE -ne $Null) -or ($env:LCOW_BASIC_MODE -ne $Null)) {
596
+    if (($null -ne $env:LCOW_MODE) -or ($null -ne $env:LCOW_BASIC_MODE)) {
597 597
         $env:LCOW_SUPPORTED=1
598 598
     }
599 599
 
... ...
@@ -606,13 +606,13 @@ Try {
606 606
     $daemonStarted=1
607 607
 
608 608
     # In LCOW mode, turn off that variable
609
-    if (($env:LCOW_MODE -ne $Null) -or ($env:LCOW_BASIC_MODE -ne $Null)) {
609
+    if (($null -ne $env:LCOW_MODE) -or ($null -ne $env:LCOW_BASIC_MODE)) {
610 610
         $env:LCOW_SUPPORTED=""
611 611
     }
612 612
 
613 613
 
614 614
     # Start tailing the daemon under test if the command is installed
615
-    if ((Get-Command "tail" -ErrorAction SilentlyContinue) -ne $null) { 
615
+    if ($null -ne (Get-Command "tail" -ErrorAction SilentlyContinue)) {
616 616
         $tail = start-process "tail" -ArgumentList "-f $env:TEMP\dut.out" -ErrorAction SilentlyContinue
617 617
     }
618 618
 
... ...
@@ -674,11 +674,11 @@ Try {
674 674
     Write-Host
675 675
 
676 676
     # Don't need Windows images when in LCOW mode.
677
-    if (($env:LCOW_MODE -eq $Null) -and ($env:LCOW_BASIC_MODE -eq $Null)) {
677
+    if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
678 678
 
679 679
         # Default to windowsservercore for the base image used for the tests. The "docker" image
680 680
         # and the control daemon use microsoft/windowsservercore regardless. This is *JUST* for the tests.
681
-        if ($env:WINDOWS_BASE_IMAGE -eq $Null) {
681
+        if ($null -eq $env:WINDOWS_BASE_IMAGE) {
682 682
             $env:WINDOWS_BASE_IMAGE="microsoft/windowsservercore"
683 683
         }
684 684
 
... ...
@@ -727,7 +727,7 @@ Try {
727 727
     }
728 728
 
729 729
     # Run the validation tests unless SKIP_VALIDATION_TESTS is defined.
730
-    if ($env:SKIP_VALIDATION_TESTS -eq $null) {
730
+    if ($null -eq $env:SKIP_VALIDATION_TESTS) {
731 731
         Write-Host -ForegroundColor Cyan "INFO: Running validation tests at $(Get-Date)..."
732 732
         $ErrorActionPreference = "SilentlyContinue"
733 733
         $Duration=$(Measure-Command { hack\make.ps1 -DCO -GoFormat -PkgImports | Out-Host })
... ...
@@ -742,8 +742,8 @@ Try {
742 742
 
743 743
     # Note the unit tests won't work in LCOW mode as I turned off loading the base images above.
744 744
     # Run the unit tests inside a container unless SKIP_UNIT_TESTS is defined
745
-    if (($env:LCOW_MODE -eq $Null) -and ($env:LCOW_BASIC_MODE -eq $Null)) {
746
-        if ($env:SKIP_UNIT_TESTS -eq $null) {
745
+    if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
746
+        if ($null -eq $env:SKIP_UNIT_TESTS) {
747 747
             Write-Host -ForegroundColor Cyan "INFO: Running unit tests at $(Get-Date)..."
748 748
             $ErrorActionPreference = "SilentlyContinue"
749 749
             $Duration=$(Measure-Command {docker run -e DOCKER_GITCOMMIT=$COMMITHASH$CommitUnsupported docker hack\make.ps1 -TestUnit | Out-Host })
... ...
@@ -758,8 +758,8 @@ Try {
758 758
     }
759 759
 
760 760
     # Add the Windows busybox image. Needed for WCOW integration tests
761
-    if (($env:LCOW_MODE -eq $Null) -and ($env:LCOW_BASIC_MODE -eq $Null)) {
762
-        if ($env:SKIP_INTEGRATION_TESTS -eq $null) {
761
+    if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
762
+        if ($null -eq $env:SKIP_INTEGRATION_TESTS) {
763 763
             $ErrorActionPreference = "SilentlyContinue"
764 764
             # Build it regardless while switching between nanoserver and windowsservercore
765 765
             #$bbCount = $(& "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" images | Select-String "busybox" | Measure-Object -line).Lines
... ...
@@ -799,8 +799,8 @@ Try {
799 799
     }
800 800
 
801 801
     # Run the WCOW integration tests unless SKIP_INTEGRATION_TESTS is defined
802
-    if (($env:LCOW_MODE -eq $Null) -and ($env:LCOW_BASIC_MODE -eq $Null)) {
803
-        if ($env:SKIP_INTEGRATION_TESTS -eq $null) {
802
+    if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
803
+        if ($null -eq $env:SKIP_INTEGRATION_TESTS) {
804 804
             Write-Host -ForegroundColor Cyan "INFO: Running integration tests at $(Get-Date)..."
805 805
             $ErrorActionPreference = "SilentlyContinue"
806 806
     
... ...
@@ -810,7 +810,7 @@ Try {
810 810
             #https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/20/solve-problems-with-external-command-lines-in-powershell/ is useful to see tokenising
811 811
             $c = "go test "
812 812
             $c += "`"-check.v`" "
813
-            if ($env:INTEGRATION_TEST_NAME -ne $null) { # Makes is quicker for debugging to be able to run only a subset of the integration tests
813
+            if ($null -ne $env:INTEGRATION_TEST_NAME) { # Makes is quicker for debugging to be able to run only a subset of the integration tests
814 814
                 $c += "`"-check.f`" "
815 815
                 $c += "`"$env:INTEGRATION_TEST_NAME`" "
816 816
                 Write-Host -ForegroundColor Magenta "WARN: Only running integration tests matching $env:INTEGRATION_TEST_NAME"
... ...
@@ -819,7 +819,7 @@ Try {
819 819
             $c += "`"-check.timeout`" " + "`"10m`" "
820 820
             $c += "`"-test.timeout`" " + "`"200m`" "
821 821
     
822
-            if ($env:INTEGRATION_IN_CONTAINER -ne $null) {
822
+            if ($null -ne $env:INTEGRATION_IN_CONTAINER) {
823 823
                 Write-Host -ForegroundColor Green "INFO: Integration tests being run inside a container"
824 824
                 # Note we talk back through the containers gateway address
825 825
                 # And the ridiculous lengths we have to go to to get the default gateway address... (GetNetIPConfiguration doesn't work in nanoserver)
... ...
@@ -853,7 +853,7 @@ Try {
853 853
         }
854 854
     } else {
855 855
         # The LCOW version of the tests here
856
-        if ($env:SKIP_INTEGRATION_TESTS -eq $null) {
856
+        if ($null -eq $env:SKIP_INTEGRATION_TESTS) {
857 857
             Write-Host -ForegroundColor Cyan "INFO: Running LCOW tests at $(Get-Date)..."
858 858
 
859 859
             $ErrorActionPreference = "SilentlyContinue"
... ...
@@ -868,7 +868,7 @@ Try {
868 868
             # Force to use the test binaries, not the host ones.
869 869
             $env:PATH="$env:TEMP\binary;$env:PATH;"  
870 870
 
871
-            if ($env:LCOW_BASIC_MODE -ne $null) {
871
+            if ($null -ne $env:LCOW_BASIC_MODE) {
872 872
                 $wc = New-Object net.webclient
873 873
                 try {
874 874
                     Write-Host -ForegroundColor green "INFO: Downloading latest execution script..."
... ...
@@ -891,7 +891,7 @@ Try {
891 891
                 #https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/20/solve-problems-with-external-command-lines-in-powershell/ is useful to see tokenising
892 892
                 $c = "go test "
893 893
                 $c += "`"-check.v`" "
894
-                if ($env:INTEGRATION_TEST_NAME -ne $null) { # Makes is quicker for debugging to be able to run only a subset of the integration tests
894
+                if ($null -ne $env:INTEGRATION_TEST_NAME) { # Makes is quicker for debugging to be able to run only a subset of the integration tests
895 895
                     $c += "`"-check.f`" "
896 896
                     $c += "`"$env:INTEGRATION_TEST_NAME`" "
897 897
                     Write-Host -ForegroundColor Magenta "WARN: Only running LCOW integration tests matching $env:INTEGRATION_TEST_NAME"
... ...
@@ -935,7 +935,7 @@ Try {
935 935
     # Stop the daemon under test
936 936
     if (Test-Path "$env:TEMP\docker.pid") {
937 937
         $p=Get-Content "$env:TEMP\docker.pid" -raw
938
-        if (($p -ne $null) -and ($daemonStarted -eq 1)) {
938
+        if (($null -ne $p) -and ($daemonStarted -eq 1)) {
939 939
             Write-Host -ForegroundColor green "INFO: Stopping daemon under test"
940 940
             taskkill -f -t -pid $p
941 941
             #sleep 5
... ...
@@ -964,14 +964,14 @@ Finally {
964 964
     Write-Host  -ForegroundColor Green "INFO: Tidying up at end of run"
965 965
 
966 966
     # Restore the path
967
-    if ($origPath -ne $null) { $env:PATH=$origPath }
967
+    if ($null -ne $origPath) { $env:PATH=$origPath }
968 968
 
969 969
     # Restore the DOCKER_HOST
970
-    if ($origDOCKER_HOST -ne $null) { $env:DOCKER_HOST=$origDOCKER_HOST }
970
+    if ($null -ne $origDOCKER_HOST) { $env:DOCKER_HOST=$origDOCKER_HOST }
971 971
 
972 972
     # Restore the GOROOT and GOPATH variables
973
-    if ($origGOROOT -ne $null) { $env:GOROOT=$origGOROOT }
974
-    if ($origGOPATH -ne $null) { $env:GOPATH=$origGOPATH }
973
+    if ($null -ne $origGOROOT) { $env:GOROOT=$origGOROOT }
974
+    if ($null -ne $origGOPATH) { $env:GOPATH=$origGOPATH }
975 975
 
976 976
     # Dump the daemon log if asked to 
977 977
     if ($daemonStarted -eq 1) {