-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathfilesys.tests.ps1
More file actions
138 lines (113 loc) · 5.63 KB
/
filesys.tests.ps1
File metadata and controls
138 lines (113 loc) · 5.63 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'FileSys resoure tests' {
BeforeAll {
$testDir = Join-Path $env:TEMP 'test-dir-resource'
$testFile = Join-Path $testDir 'test-file-resource.txt'
$testFileName = 'test-file-resource.txt'
}
It 'Filesys resource can create file' {
if (Test-Path $testFile) {
Remove-Item -Path $testFile -Force
}
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$path = $resultJson.results.result.afterState.path
$path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
}
It 'Filesys resource can create directory' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_create.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultJson.results.result.afterState.path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
}
It 'Filesys resource can create file with content' {
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_filecontent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultFilePath = $resultJson.results.result.afterState.path
$resultFilePath | Should -Exist
Get-Content $resultFilePath | Should -Be "Hello, World!"
}
It 'Filesys resource can delete a file' {
if (-not (Test-Path $testFile)) {
New-Item -Path $testFile -ItemType File -Force | Out-Null
}
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_delete.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultFilePath = $resultJson.results.result.afterState.path
$resultFilePath | Should -Not -Exist
}
It 'Filesys resource can delete an empty directory' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultDirPath = $resultJson.results.result.afterState.path
$resultDirPath | Should -Not -Exist
}
It 'Filesys resource cannot delete a non-empty directory' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
New-Item -Path (Join-Path $testDir $testFileName) -ItemType File -Force | Out-Null
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Not -Be 0
$testDir | Should -Exist
}
It 'Filesys resource can delete a directory recursively' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
$dirPath = New-Item -Path $testDir -ItemType Directory -Force
$subDirPath = New-Item -Path (Join-Path $dirPath 'test-subdir') -ItemType Directory -Force
New-Item -Path (Join-Path $subDirPath $testFileName) -ItemType File -Force | Out-Null
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete_recurse.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultDirPath = $resultJson.results.result.afterState.path
$resultDirPath | Should -Not -Exist
}
It 'Can create file if parent directory does not exist' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create_parent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultJson.results.result.afterState.path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
}
It 'Can create file with content if parent directory does not exist' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_filecontent_parent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultFilePath = $resultJson.results.result.afterState.path
$resultFilePath | Should -Exist
Get-Content $resultFilePath | Should -Be "Hello, World!"
}
It 'Can create directory if parent directory does not exist' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_create_parent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultJson.results.result.afterState.path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
}
}