Skip to content

Commit a6ff0bf

Browse files
Fixes and tests
1 parent c8a1d53 commit a6ff0bf

17 files changed

+351
-174
lines changed

dsc/examples/filesys_create.dsc.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ resources:
44
- name: Create empty file
55
type: Microsoft.DSC/File
66
properties:
7-
path: "[envvar('TEMP')]"
8-
name: 'test-file-resource.txt'
7+
path: "[path(envvar('TEMP'), 'test-file-resource.txt')]"
98
_exist: true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Example configuration mixing native app resources with classic PS resources
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
3+
resources:
4+
- name: Create empty file
5+
type: Microsoft.DSC/File
6+
properties:
7+
path: "[path(envvar('TEMP'), 'test-dir-resource', 'test-file-resource.txt')]"
8+
_exist: true

dsc/examples/filesys_delete.dsc.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ resources:
44
- name: Create empty file
55
type: Microsoft.DSC/File
66
properties:
7-
path: '[path(envvar('TEMP'))]''
8-
name: 'test-file-resource.txt'
7+
path: "[path(envvar('TEMP'), 'test-file-resource.txt')]"
98
_exist: false

dsc/examples/filesys_dir_create.dsc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ resources:
44
- name: Create empty directory
55
type: Microsoft.DSC/Directory
66
properties:
7-
path: "[path(envvar('TEMP'), 'test-directory')]"
7+
path: "[path(envvar('TEMP'), 'test-dir-resource')]"
88
_exist: true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Example configuration mixing native app resources with classic PS resources
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
3+
resources:
4+
- name: Create empty directory
5+
type: Microsoft.DSC/Directory
6+
properties:
7+
path: "[path(envvar('TEMP'), 'test-dir-resource', 'test-sub-dir-resource')]"
8+
_exist: true

dsc/examples/filesys_filecontent.dsc.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ resources:
44
- name: Set file content
55
type: Microsoft.DSC/FileContent
66
properties:
7-
path: '[path(envvar('TEMP'))]''
8-
name: 'test-file-resource.txt'
7+
path: "[path(envvar('TEMP'), 'test-file-resource.txt')]"
98
content: "Hello, World!"
109
_exist: true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Example configuration mixing native app resources with classic PS resources
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
3+
resources:
4+
- name: Set file content
5+
type: Microsoft.DSC/FileContent
6+
properties:
7+
path: "[path(envvar('TEMP'), 'test-dir-resource', 'test-file-resource.txt')]"
8+
content: "Hello, World!"
9+
_exist: true

dsc/tests/filesys.tests.ps1

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,28 @@ Describe 'FileSys resoure tests' {
1414
}
1515

1616
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create.dsc.yaml" | ConvertFrom-Json
17+
$LASTEXITCODE | Should -Be 0
1718
$resultJson.hadErrors | Should -BeFalse
1819
$path = $resultJson.results.result.afterState.path
19-
$name = $resultJson.results.result.afterState.name
20-
21-
$path | Should -Be $env:TEMP
22-
(Join-Path $path $name) | Should -Exist
20+
$path | Should -Exist
2321
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
2422
}
2523

2624
It 'Filesys resource can create directory' {
27-
$resultJson = dsc config set -f "../examples/filesys_dir_create.dsc.yaml" | ConvertFrom-Json
25+
if (Test-Path $testDir) {
26+
Remove-Item -Path $testDir -Force -Recurse
27+
}
28+
29+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_create.dsc.yaml" | ConvertFrom-Json
30+
$LASTEXITCODE | Should -Be 0
2831
$resultJson.hadErrors | Should -BeFalse
2932
$resultJson.results.result.afterState.path | Should -Exist
3033
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
31-
3234
}
3335

3436
It 'Filesys resource can create file with content' {
35-
$resultJson = dsc config set -f "../examples/filesys_filecontent.dsc.yaml" | ConvertFrom-Json
37+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_filecontent.dsc.yaml" | ConvertFrom-Json
38+
$LASTEXITCODE | Should -Be 0
3639
$resultJson.hadErrors | Should -BeFalse
3740

3841
$resultFilePath = $resultJson.results.result.afterState.path
@@ -45,45 +48,93 @@ Describe 'FileSys resoure tests' {
4548
New-Item -Path $testFile -ItemType File -Force | Out-Null
4649
}
4750

48-
$resultJson = dsc config set -f "../examples/filesys_delete.dsc.yaml" | ConvertFrom-Json
51+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_delete.dsc.yaml" | ConvertFrom-Json
52+
$LASTEXITCODE | Should -Be 0
4953
$resultJson.hadErrors | Should -BeFalse
5054
$resultFilePath = $resultJson.results.result.afterState.path
5155
$resultFilePath | Should -Not -Exist
5256
}
5357

54-
It 'Filesys resource can delete an empty directory' -Pending {
55-
if (-not (Test-Path $testDir)) {
56-
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
58+
It 'Filesys resource can delete an empty directory' {
59+
if (Test-Path $testDir) {
60+
Remove-Item -Path $testDir -Force -Recurse
5761
}
5862

59-
$resultJson = dsc config set -f "../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
63+
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
64+
65+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
66+
$LASTEXITCODE | Should -Be 0
6067
$resultJson.hadErrors | Should -BeFalse
6168
$resultDirPath = $resultJson.results.result.afterState.path
6269
$resultDirPath | Should -Not -Exist
6370
}
6471

65-
It 'Filesys resource can delete a non-empty directory' -Pending {
66-
if (-not (Test-Path $testDir)) {
67-
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
68-
New-Item -Path (Join-Path $testDir $testFileName) -ItemType File -Force | Out-Null
72+
It 'Filesys resource cannot delete a non-empty directory' -Pending {
73+
if (Test-Path $testDir) {
74+
Remove-Item -Path $testDir -Force -Recurse
6975
}
7076

71-
$resultJson = dsc config set -f "../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
77+
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
78+
New-Item -Path (Join-Path $testDir $testFileName) -ItemType File -Force | Out-Null
79+
80+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
81+
$LASTEXITCODE | Should -Be 0
7282
$resultJson.hadErrors | Should -BeFalse
7383
$resultDirPath = $resultJson.results.result.afterState.path
7484
$resultDirPath | Should -Not -Exist
7585
}
7686

77-
It 'Filesys resource can delete a directory recursively' -Pending {
78-
if (-not (Test-Path $testDir)) {
79-
$dirPath = New-Item -Path $testDir -ItemType Directory -Force | Out-Null
80-
$subDirPath = New-Item -Path (Join-Path $dirPath 'test-subdir') -ItemType Directory -Force | Out-Null
81-
New-Item -Path (Join-Path $subDirPath $testFileName) -ItemType File -Force | Out-Null
87+
It 'Filesys resource can delete a directory recursively' {
88+
if (Test-Path $testDir) {
89+
Remove-Item -Path $testDir -Force -Recurse
8290
}
8391

84-
$resultJson = dsc config set -f "../examples/filesys_dir_delete_recursive.dsc.yaml" | ConvertFrom-Json
92+
$dirPath = New-Item -Path $testDir -ItemType Directory -Force
93+
$subDirPath = New-Item -Path (Join-Path $dirPath 'test-subdir') -ItemType Directory -Force
94+
New-Item -Path (Join-Path $subDirPath $testFileName) -ItemType File -Force | Out-Null
95+
96+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete_recurse.dsc.yaml" | ConvertFrom-Json
97+
$LASTEXITCODE | Should -Be 0
8598
$resultJson.hadErrors | Should -BeFalse
8699
$resultDirPath = $resultJson.results.result.afterState.path
87100
$resultDirPath | Should -Not -Exist
88101
}
102+
103+
It 'Can create file if parent directory does not exist' {
104+
if (Test-Path $testDir) {
105+
Remove-Item -Path $testDir -Force -Recurse
106+
}
107+
108+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create_parent.dsc.yaml" | ConvertFrom-Json
109+
$LASTEXITCODE | Should -Be 0
110+
$resultJson.hadErrors | Should -BeFalse
111+
$resultJson.results.result.afterState.path | Should -Exist
112+
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
113+
}
114+
115+
It 'Can create file with content if parent directory does not exist' {
116+
if (Test-Path $testDir) {
117+
Remove-Item -Path $testDir -Force -Recurse
118+
}
119+
120+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_filecontent_parent.dsc.yaml" | ConvertFrom-Json
121+
$LASTEXITCODE | Should -Be 0
122+
$resultJson.hadErrors | Should -BeFalse
123+
124+
$resultFilePath = $resultJson.results.result.afterState.path
125+
$resultFilePath | Should -Exist
126+
Get-Content $resultFilePath | Should -Be "Hello, World!"
127+
}
128+
129+
It 'Can create directory if parent directory does not exist' {
130+
if (Test-Path $testDir) {
131+
Remove-Item -Path $testDir -Force -Recurse
132+
}
133+
134+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_create_parent.dsc.yaml" | ConvertFrom-Json
135+
$LASTEXITCODE | Should -Be 0
136+
$resultJson.hadErrors | Should -BeFalse
137+
$resultJson.results.result.afterState.path | Should -Exist
138+
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
139+
}
89140
}

resources/filesys/directory.dsc.resource.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
{
1111
"jsonInputArg": "--input",
1212
"mandatory": true
13-
}
13+
},
14+
"--schema-type",
15+
"directory"
1416
],
1517
"input": "stdin",
1618
"implementsPretest": false
@@ -22,7 +24,9 @@
2224
{
2325
"jsonInputArg": "--input",
2426
"mandatory": true
25-
}
27+
},
28+
"--schema-type",
29+
"directory"
2630
],
2731
"input": "stdin"
2832
},
@@ -33,7 +37,9 @@
3337
{
3438
"jsonInputArg": "--input",
3539
"mandatory": true
36-
}
40+
},
41+
"--schema-type",
42+
"directory"
3743
],
3844
"input": "stdin"
3945
},
@@ -44,7 +50,9 @@
4450
{
4551
"jsonInputArg": "--input",
4652
"mandatory": true
47-
}
53+
},
54+
"--schema-type",
55+
"directory"
4856
],
4957
"input": "stdin"
5058
},

resources/filesys/file.dsc.resource.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
{
1111
"jsonInputArg": "--input",
1212
"mandatory": true
13-
}
13+
},
14+
"--schema-type",
15+
"file"
1416
],
1517
"input": "stdin",
1618
"implementsPretest": false
@@ -22,7 +24,9 @@
2224
{
2325
"jsonInputArg": "--input",
2426
"mandatory": true
25-
}
27+
},
28+
"--schema-type",
29+
"file"
2630
],
2731
"input": "stdin"
2832
},
@@ -33,7 +37,9 @@
3337
{
3438
"jsonInputArg": "--input",
3539
"mandatory": true
36-
}
40+
},
41+
"--schema-type",
42+
"file"
3743
],
3844
"input": "stdin"
3945
},
@@ -44,7 +50,9 @@
4450
{
4551
"jsonInputArg": "--input",
4652
"mandatory": true
47-
}
53+
},
54+
"--schema-type",
55+
"file"
4856
],
4957
"input": "stdin"
5058
},

resources/filesys/filecontent.dsc.resource.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
{
1111
"jsonInputArg": "--input",
1212
"mandatory": true
13-
}
13+
},
14+
"--schema-type",
15+
"file-content"
1416
],
1517
"input": "stdin",
1618
"implementsPretest": false
@@ -22,7 +24,9 @@
2224
{
2325
"jsonInputArg": "--input",
2426
"mandatory": true
25-
}
27+
},
28+
"--schema-type",
29+
"file-content"
2630
],
2731
"input": "stdin"
2832
},
@@ -33,7 +37,9 @@
3337
{
3438
"jsonInputArg": "--input",
3539
"mandatory": true
36-
}
40+
},
41+
"--schema-type",
42+
"file-content"
3743
],
3844
"input": "stdin"
3945
},

resources/filesys/src/args.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,32 @@ pub enum SubCommand {
1717
Get {
1818
#[clap(short, long, required = true, help = "The path to the file.")]
1919
input: String,
20+
#[clap(short, long, default_value = "file", help = "The type of file system resource.")]
21+
schema_type: FileSystemObjectType,
2022
},
2123

2224
#[clap(name = "delete", about = "Delete the file on disk.", arg_required_else_help = true)]
2325
Delete {
2426
#[clap(short, long, required = true, help = "The path to the file.")]
2527
input: String,
28+
#[clap(short, long, default_value = "file", help = "The type of file system resource.")]
29+
schema_type: FileSystemObjectType,
2630
},
2731

2832
#[clap(name = "set", about = "Set the current state of file or directory.", arg_required_else_help = true)]
2933
Set {
3034
#[clap(short, long, required = true, help = "The path to the file or directory.")]
3135
input : String,
36+
#[clap(short, long, default_value = "file", help = "The type of file system resource.")]
37+
schema_type: FileSystemObjectType,
3238
},
3339

3440
#[clap(name = "export", about = "Exports the files and directories under the specified path", arg_required_else_help = true)]
3541
Export {
3642
#[clap(short, long, required = true, help = "The path to the file or directory.")]
3743
input: String,
44+
#[clap(short, long, default_value = "file", help = "The type of file system resource.")]
45+
schema_type: FileSystemObjectType,
3846
},
3947

4048
#[clap(name = "schema", about = "Retrieve JSON schema.")]

resources/filesys/src/config.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ pub struct File {
1010
/// The path to the file.
1111
pub path: String,
1212

13-
pub name: String,
14-
1513
/// The file size.
1614
#[serde(skip_serializing_if = "Option::is_none")]
1715
pub size: Option<u64>,
@@ -52,9 +50,6 @@ pub struct FileContent
5250
/// The path to the file.
5351
pub path: String,
5452

55-
/// The file name.
56-
pub name: String,
57-
5853
/// The file hash. If not provided, the hash is calculated from the content.
5954
#[serde(skip_serializing_if = "Option::is_none")]
6055
pub hash: Option<String>,

0 commit comments

Comments
 (0)