MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "UnixFileSystem additions" -Tag "CI" {4 Context "Basic Validation" {5 BeforeAll {6 $PSDefaultParameterValues.Add('It:Skip', $IsWindows)7 }8 9 AfterAll {10 $PSDefaultParameterValues.Remove('It:Skip')11 }12 13 It "Should include a UnixStat property" {14 $i = Get-Item ${TestDrive}15 $i.UnixStat | Should -Not -BeNullOrEmpty16 }17 18 It "The UnixStat property should be the correct type" {19 $expected = "System.Management.Automation.Platform+Unix+CommonStat"20 $i = (Get-Item /).psobject.properties['UnixStat'].TypeNameOfValue21 $i | Should -Be $expected22 }23 }24 25 Context "Validation of additional properties on file system objects" {26 BeforeAll {27 $PSDefaultParameterValues.Add('It:Skip', $IsWindows)28 29 $testDir = "${TestDrive}/TestDir"30 $testFile = "${testDir}/TestFile"31 32 $testCase = @{ Mode = '000'; Perm = '----------'; Item = "${testFile}" },33 @{ Mode = '111'; Perm = '---x--x--x'; Item = "${testFile}" },34 @{ Mode = '222'; Perm = '--w--w--w-'; Item = "${testFile}" },35 @{ Mode = '333'; Perm = '--wx-wx-wx'; Item = "${testFile}" },36 @{ Mode = '444'; Perm = '-r--r--r--'; Item = "${testFile}" },37 @{ Mode = '555'; Perm = '-r-xr-xr-x'; Item = "${testFile}" },38 @{ Mode = '666'; Perm = '-rw-rw-rw-'; Item = "${testFile}" },39 @{ Mode = '777'; Perm = '-rwxrwxrwx'; Item = "${testFile}" },40 @{ Mode = '4644'; Perm = '-rwSr--r--'; Item = "${testFile}" },41 @{ Mode = '1644'; Perm = '-rw-r--r-T'; Item = "${testFile}" },42 @{ Mode = '2644'; Perm = '-rw-r-Sr--'; Item = "${testFile}" },43 @{ Mode = '7644'; Perm = '-rwSr-Sr-T'; Item = "${testFile}" },44 @{ Mode = '4777'; Perm = '-rwsrwxrwx'; Item = "${testFile}" },45 @{ Mode = '1777'; Perm = 'drwxrwxrwt'; Item = "${testDir}" }46 }47 48 AfterAll {49 $PSDefaultParameterValues.Remove('It:Skip')50 }51 52 BeforeEach {53 $null = New-Item -ItemType Directory -Path "${testDir}"54 $null = New-Item -ItemType File -Path "${testFile}"55 }56 57 AfterEach {58 Remove-Item -Path "${testFile}" -Force59 Remove-Item -Path "${testDir}" -Recurse -Force60 }61 62 It "Should present filemode '<Mode>' string correctly as '<Perm>'" -testCase $testCase {63 param ($Mode, $Perm, $Item )64 # chmod can fail for some modes so be sure to handle that here.65 # specifically, when setting setgid chmod can fail if the group is privileged.66 chmod "$Mode" "${Item}"67 if ($LASTEXITCODE -ne 0) {68 set-itresult -skip -because "chmod '$mode' failed"69 }70 else {71 $i = Get-Item $Item72 $i.UnixMode | Should -BeExactly $Perm73 }74 }75 76 It "Should retrieve the user name for the file" {77 $i = Get-Item ${testFile}78 $user = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[2]79 $i.User | Should -Be $user80 }81 82 It "Should retrieve the group name for the file" {83 $i = Get-Item ${testFile}84 $group = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[3]85 $i.Group | Should -Be $Group86 }87 }88 89 Context "Other properties of UnixStat object" {90 BeforeAll {91 $PSDefaultParameterValues.Add('It:Skip', $IsWindows)92 if ($IsWindows) {93 return94 }95 96 $testDir = "${TestDrive}/TestDir"97 $testFile = "${testDir}/TestFile"98 99 $null = New-Item -Type Directory -Path $testDir100 $null = New-Item -Type File -Path $testFile101 Set-Content -Path ${testFile} -Value "abc"102 103 $expectedFileInode,$permission,$expectedFileLinkCount,$user,$group,$expectedFileSize,$unused =104 (/bin/ls -ldi ${testFile}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries")105 $file = Get-Item ${testFile}106 107 $expectedDirInode,$permission,$expectedDirLinkCount,$user,$group,$expectedDirSize,$unused =108 (/bin/ls -ldi ${testDir}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries")109 $Dir = Get-Item ${testDir}110 111 $testCases =112 @{ Expected = $expectedFileInode; Observed = $File.UnixStat.Inode; Title = "FileInode" },113 @{ Expected = $expectedFileLinkCount; Observed = $File.UnixStat.HardlinkCount; Title = "FileHardlinkCount" },114 @{ Expected = $expectedFileSize; Observed = $File.UnixStat.Size; Title = "FileSize" },115 @{ Expected = $expectedDirInode; Observed = $Dir.UnixStat.Inode; Title = "DirInode" },116 @{ Expected = $expectedDirLinkCount; Observed = $Dir.UnixStat.HardlinkCount; Title = "DirHardlinkCount" },117 @{ Expected = $expectedDirSize; Observed = $Dir.UnixStat.Size; Title = "DirSize" }118 }119 120 AfterAll {121 $PSDefaultParameterValues.Remove('It:Skip')122 }123 124 It "Should have correct values in UnixStat property for '<Title>'" -TestCases $testCases {125 param ( $Title, $expected, $observed )126 127 $observed | Should -Be $expected128 129 }130 }131 132}133 