Skip to content

Commit c8a1d53

Browse files
Fix file resource
1 parent b1f744c commit c8a1d53

13 files changed

+145
-43
lines changed

dsc/examples/filesys_create.dsc.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Example configuration mixing native app resources with classic PS resources
2-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
33
resources:
44
- name: Create empty file
55
type: Microsoft.DSC/File
66
properties:
7-
path: "[path(envar(TEMP), 'test-file-resource.txt')]"
7+
path: "[envvar('TEMP')]"
8+
name: 'test-file-resource.txt'
89
_exist: true

dsc/examples/filesys_delete.dsc.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Example configuration mixing native app resources with classic PS resources
2-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
33
resources:
44
- name: Create empty file
55
type: Microsoft.DSC/File
66
properties:
7-
path: "[path(envar(TEMP), 'test-file-resource.txt')]"
7+
path: '[path(envvar('TEMP'))]''
8+
name: 'test-file-resource.txt'
89
_exist: false
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Example configuration mixing native app resources with classic PS resources
2-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
33
resources:
44
- name: Create empty directory
55
type: Microsoft.DSC/Directory
66
properties:
7-
path: "[path(envar(TEMP), 'test-file-resource.txt')]"
7+
path: "[path(envvar('TEMP'), 'test-directory')]"
88
_exist: true
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Example configuration mixing native app resources with classic PS resources
2-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
33
resources:
44
- name: Delete empty directory
55
type: Microsoft.DSC/Directory
66
properties:
7-
path: "[path(envar(TEMP), 'testdir')]"
7+
path: "[path(envvar('TEMP'), 'test-dir-resource')]"
88
_exist: false
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Example configuration mixing native app resources with classic PS resources
2-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
33
resources:
44
- name: Delete non-empty directory
55
type: Microsoft.DSC/Directory
66
properties:
7-
path: "[path(envar(TEMP), 'testdir')]"
7+
path: "[path(envvar('TEMP'), 'test-dir-resource')]"
88
recurse: true
99
_exist: false
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Example configuration mixing native app resources with classic PS resources
2-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
33
resources:
44
- name: Set file content
55
type: Microsoft.DSC/FileContent
66
properties:
7-
path: "[path(envar(TEMP), 'test-file-resource.txt')]"
7+
path: '[path(envvar('TEMP'))]''
8+
name: 'test-file-resource.txt'
89
content: "Hello, World!"
910
_exist: true

dsc/tests/filesys.tests.ps1

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'FileSys resoure tests' {
5+
BeforeAll {
6+
$testDir = Join-Path $env:TEMP 'test-dir-resource'
7+
$testFile = Join-Path $testDir 'test-file-resource.txt'
8+
$testFileName = 'test-file-resource.txt'
9+
}
10+
11+
It 'Filesys resource can create file' {
12+
if (Test-Path $testFile) {
13+
Remove-Item -Path $testFile -Force
14+
}
15+
16+
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create.dsc.yaml" | ConvertFrom-Json
17+
$resultJson.hadErrors | Should -BeFalse
18+
$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
23+
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
24+
}
25+
26+
It 'Filesys resource can create directory' {
27+
$resultJson = dsc config set -f "../examples/filesys_dir_create.dsc.yaml" | ConvertFrom-Json
28+
$resultJson.hadErrors | Should -BeFalse
29+
$resultJson.results.result.afterState.path | Should -Exist
30+
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
31+
32+
}
33+
34+
It 'Filesys resource can create file with content' {
35+
$resultJson = dsc config set -f "../examples/filesys_filecontent.dsc.yaml" | ConvertFrom-Json
36+
$resultJson.hadErrors | Should -BeFalse
37+
38+
$resultFilePath = $resultJson.results.result.afterState.path
39+
$resultFilePath | Should -Exist
40+
Get-Content $resultFilePath | Should -Be "Hello, World!"
41+
}
42+
43+
It 'Filesys resource can delete a file' {
44+
if (-not (Test-Path $testFile)) {
45+
New-Item -Path $testFile -ItemType File -Force | Out-Null
46+
}
47+
48+
$resultJson = dsc config set -f "../examples/filesys_delete.dsc.yaml" | ConvertFrom-Json
49+
$resultJson.hadErrors | Should -BeFalse
50+
$resultFilePath = $resultJson.results.result.afterState.path
51+
$resultFilePath | Should -Not -Exist
52+
}
53+
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
57+
}
58+
59+
$resultJson = dsc config set -f "../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
60+
$resultJson.hadErrors | Should -BeFalse
61+
$resultDirPath = $resultJson.results.result.afterState.path
62+
$resultDirPath | Should -Not -Exist
63+
}
64+
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
69+
}
70+
71+
$resultJson = dsc config set -f "../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
72+
$resultJson.hadErrors | Should -BeFalse
73+
$resultDirPath = $resultJson.results.result.afterState.path
74+
$resultDirPath | Should -Not -Exist
75+
}
76+
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
82+
}
83+
84+
$resultJson = dsc config set -f "../examples/filesys_dir_delete_recursive.dsc.yaml" | ConvertFrom-Json
85+
$resultJson.hadErrors | Should -BeFalse
86+
$resultDirPath = $resultJson.results.result.afterState.path
87+
$resultDirPath | Should -Not -Exist
88+
}
89+
}

pal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ opt-level = 2
1313
lto = true
1414

1515
[build-dependencies]
16-
cc = "1.1"
16+
cc = "~1.1"

resources/filesys/src/config.rs

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

13+
pub name: String,
14+
1315
/// The file size.
1416
#[serde(skip_serializing_if = "Option::is_none")]
1517
pub size: Option<u64>,
@@ -50,6 +52,9 @@ pub struct FileContent
5052
/// The path to the file.
5153
pub path: String,
5254

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

resources/filesys/src/dir_helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn export_dir_path(dir: &Directory) -> Result<Directory, Box<dyn std::error:
9898
for entry in dir {
9999
let entry = entry?;
100100
let path = entry.path();
101-
let f = File::new(path.to_str().unwrap());
101+
let f = File::new(path.to_str().unwrap(), path.file_name().unwrap().to_str().unwrap());
102102
files.push(get_file(&f)?);
103103
}
104104
files
@@ -110,7 +110,7 @@ pub fn export_dir_path(dir: &Directory) -> Result<Directory, Box<dyn std::error:
110110
}
111111
false => {
112112
let path = Path::new(path);
113-
let f = File::new(path.to_str().unwrap());
113+
let f = File::new(path.to_str().unwrap(), path.file_name().unwrap().to_str().unwrap());
114114
let file = get_file(&f)?;
115115
let parent = path.parent();
116116
match parent {

0 commit comments

Comments
 (0)