|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +Describe "UnixFileSystem additions" -Tag "CI" { |
| 4 | + # if PSUnixFileStat is converted from an experimental feature, these tests will need to be changed |
| 5 | + BeforeAll { |
| 6 | + $experimentalFeatureName = "PSUnixFileStat" |
| 7 | + $skipTest = -not $EnabledExperimentalFeatures.Contains($experimentalFeatureName) |
| 8 | + $PSDefaultParameterValues.Add('It:Skip', $skipTest) |
| 9 | + } |
| 10 | + AfterAll { |
| 11 | + $PSDefaultParameterValues.Remove('It:Skip') |
| 12 | + } |
| 13 | + Context "Basic Validation" { |
| 14 | + |
| 15 | + It "Should be an experimental feature on non-Windows systems" { |
| 16 | + $feature = Get-ExperimentalFeature -Name $experimentalFeatureName |
| 17 | + if ( $IsWindows ) { |
| 18 | + $feature | Should -BeNullOrEmpty |
| 19 | + } |
| 20 | + else { |
| 21 | + $feature.Name | Should -Be $experimentalFeatureName |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + It "Should include a UnixStat property" { |
| 26 | + $i = Get-Item ${TestDrive} |
| 27 | + $i.UnixStat | Should -Not -BeNullOrEmpty |
| 28 | + } |
| 29 | + |
| 30 | + It "The UnixStat property should be the correct type" { |
| 31 | + $expected = "System.Management.Automation.Platform+Unix+CommonStat" |
| 32 | + $i = (get-item /).psobject.properties['UnixStat'].TypeNameOfValue |
| 33 | + $i | Should -Be $expected |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + Context "Validation of additional properties on file system objects" { |
| 38 | + BeforeAll { |
| 39 | + if ( $IsWindows ) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + $testDir = "${TestDrive}/TestDir" |
| 44 | + $testFile = "${testDir}/TestFile" |
| 45 | + |
| 46 | + $testCase = @{ Mode = '000'; Perm = '----------'; Item = "${testFile}" }, |
| 47 | + @{ Mode = '111'; Perm = '---x--x--x'; Item = "${testFile}" }, |
| 48 | + @{ Mode = '222'; Perm = '--w--w--w-'; Item = "${testFile}" }, |
| 49 | + @{ Mode = '333'; Perm = '--wx-wx-wx'; Item = "${testFile}" }, |
| 50 | + @{ Mode = '444'; Perm = '-r--r--r--'; Item = "${testFile}" }, |
| 51 | + @{ Mode = '555'; Perm = '-r-xr-xr-x'; Item = "${testFile}" }, |
| 52 | + @{ Mode = '666'; Perm = '-rw-rw-rw-'; Item = "${testFile}" }, |
| 53 | + @{ Mode = '777'; Perm = '-rwxrwxrwx'; Item = "${testFile}" }, |
| 54 | + @{ Mode = '4777'; Perm = '-rwsrwxrwx'; Item = "${testFile}" }, |
| 55 | + @{ Mode = '1777'; Perm = 'drwxrwxrwt'; Item = "${testDir}" } |
| 56 | + } |
| 57 | + |
| 58 | + BeforeEach { |
| 59 | + $null = New-Item -ItemType Directory -Path "${testDir}" |
| 60 | + $null = New-Item -ItemType File -Path "${testFile}" |
| 61 | + } |
| 62 | + |
| 63 | + AfterEach { |
| 64 | + Remove-Item -Path "${testFile}" -Force |
| 65 | + Remove-Item -Path "${testDir}" -Recurse -Force |
| 66 | + } |
| 67 | + |
| 68 | + It "Should present filemode '<Mode>' string correctly as '<Perm>'" -testCase $testCase { |
| 69 | + param ($Mode, $Perm, $Item ) |
| 70 | + chmod "$Mode" "${Item}" |
| 71 | + $i = Get-Item $Item |
| 72 | + $i.UnixMode | Should -Be $Perm |
| 73 | + } |
| 74 | + |
| 75 | + It "Should retrieve the user name for the file" { |
| 76 | + $i = Get-Item ${testFile} |
| 77 | + $user = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[2] |
| 78 | + $i.User | Should -Be $user |
| 79 | + } |
| 80 | + |
| 81 | + It "Should retrieve the group name for the file" { |
| 82 | + $i = Get-Item ${testFile} |
| 83 | + $group = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[3] |
| 84 | + $i.Group | Should -Be $Group |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + Context "Other properties of UnixStat object" { |
| 89 | + BeforeAll { |
| 90 | + if ( $IsWindows ) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + $testDir = "${TestDrive}/TestDir" |
| 95 | + $testFile = "${testDir}/TestFile" |
| 96 | + |
| 97 | + $null = New-Item -Type Directory -Path $testDir |
| 98 | + $null = New-Item -Type File -Path $testFile |
| 99 | + Set-Content -Path ${testFile} -Value "abc" |
| 100 | + |
| 101 | + $expectedFileInode,$permission,$expectedFileLinkCount,$user,$group,$expectedFileSize,$unused = |
| 102 | + (/bin/ls -ldi ${testFile}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries") |
| 103 | + $file = Get-Item ${testFile} |
| 104 | + |
| 105 | + $expectedDirInode,$permission,$expectedDirLinkCount,$user,$group,$expectedDirSize,$unused = |
| 106 | + (/bin/ls -ldi ${testDir}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries") |
| 107 | + $Dir = Get-Item ${testDir} |
| 108 | + |
| 109 | + $testCases = |
| 110 | + @{ Expected = $expectedFileInode; Observed = $File.UnixStat.Inode; Title = "FileInode" }, |
| 111 | + @{ Expected = $expectedFileLinkCount; Observed = $File.UnixStat.HardlinkCount; Title = "FileHardlinkCount" }, |
| 112 | + @{ Expected = $expectedFileSize; Observed = $File.UnixStat.Size; Title = "FileSize" }, |
| 113 | + @{ Expected = $expectedDirInode; Observed = $Dir.UnixStat.Inode; Title = "DirInode" }, |
| 114 | + @{ Expected = $expectedDirLinkCount; Observed = $Dir.UnixStat.HardlinkCount; Title = "DirHardlinkCount" }, |
| 115 | + @{ Expected = $expectedDirSize; Observed = $Dir.UnixStat.Size; Title = "DirSize" } |
| 116 | + } |
| 117 | + |
| 118 | + It "Should have correct values in UnixStat property for '<Title>'" -TestCases $testCases { |
| 119 | + param ( $Title, $expected, $observed ) |
| 120 | + |
| 121 | + $observed | Should -Be $expected |
| 122 | + |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments