Skip to content

Commit 6550314

Browse files
Fixes and tests
1 parent a6ff0bf commit 6550314

File tree

2 files changed

+206
-48
lines changed

2 files changed

+206
-48
lines changed

dsc/tests/filesys.tests.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Describe 'FileSys resoure tests' {
6969
$resultDirPath | Should -Not -Exist
7070
}
7171

72-
It 'Filesys resource cannot delete a non-empty directory' -Pending {
72+
It 'Filesys resource cannot delete a non-empty directory' {
7373
if (Test-Path $testDir) {
7474
Remove-Item -Path $testDir -Force -Recurse
7575
}
@@ -78,10 +78,8 @@ Describe 'FileSys resoure tests' {
7878
New-Item -Path (Join-Path $testDir $testFileName) -ItemType File -Force | Out-Null
7979

8080
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
81-
$LASTEXITCODE | Should -Be 0
82-
$resultJson.hadErrors | Should -BeFalse
83-
$resultDirPath = $resultJson.results.result.afterState.path
84-
$resultDirPath | Should -Not -Exist
81+
$LASTEXITCODE | Should -Not -Be 0
82+
$testDir | Should -Exist
8583
}
8684

8785
It 'Filesys resource can delete a directory recursively' {

resources/filesys/src/main.rs

Lines changed: 203 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ mod dir_helpers;
1818
mod filecontent_helper;
1919

2020
const EXIT_SUCCESS: i32 = 0;
21-
const EXIT_INVALID_INPUT: i32 = 2;
21+
const EXIT_INVALID_INPUT: i32 = 1;
22+
const EXIT_JSON_SERIALIZATION_FAILED: i32 = 2;
2223

2324
fn main() {
2425
let args = Args::parse();
@@ -30,9 +31,21 @@ fn main() {
3031
debug!("Getting file at path: {}", input);
3132
match parse_file(input) {
3233
Some(parsed_file) => {
33-
let file = get_file(&parsed_file).unwrap();
34-
let json = serde_json::to_string(&file).unwrap();
35-
println!("{}", json);
34+
match get_file(&parsed_file) {
35+
Ok(file) => {
36+
match serde_json::to_string(&file) {
37+
Ok(json) => println!("{}", json),
38+
Err(e) => {
39+
error!("Failed to serialize file: {}", e);
40+
exit(EXIT_JSON_SERIALIZATION_FAILED);
41+
}
42+
}
43+
}
44+
Err(e) => {
45+
error!("Failed to get file: {}", e);
46+
exit(EXIT_INVALID_INPUT);
47+
}
48+
}
3649
}
3750
None => {
3851
error!("Invalid input for file.");
@@ -44,9 +57,21 @@ fn main() {
4457
debug!("Getting directory at path: {}", input);
4558
match parse_directory(input) {
4659
Some(parsed_directory) => {
47-
let dir = get_dir(&parsed_directory).unwrap();
48-
let json = serde_json::to_string(&dir).unwrap();
49-
println!("{}", json);
60+
match get_dir(&parsed_directory) {
61+
Ok(dir) => {
62+
match serde_json::to_string(&dir) {
63+
Ok(json) => println!("{}", json),
64+
Err(e) => {
65+
error!("Failed to serialize directory: {}", e);
66+
exit(EXIT_JSON_SERIALIZATION_FAILED);
67+
}
68+
}
69+
}
70+
Err(e) => {
71+
error!("Failed to get directory: {}", e);
72+
exit(EXIT_INVALID_INPUT);
73+
}
74+
}
5075
}
5176
None => {
5277
error!("Invalid input for directory.");
@@ -58,9 +83,21 @@ fn main() {
5883
debug!("Getting file content at path: {}", input);
5984
match parse_filecontent(input) {
6085
Some(parsed_filecontent) => {
61-
let filecontent = get_file_content(&parsed_filecontent).unwrap();
62-
let json = serde_json::to_string(&filecontent).unwrap();
63-
println!("{}", json);
86+
match get_file_content(&parsed_filecontent) {
87+
Ok(filecontent) => {
88+
match serde_json::to_string(&filecontent) {
89+
Ok(json) => println!("{}", json),
90+
Err(e) => {
91+
error!("Failed to serialize file content: {}", e);
92+
exit(EXIT_JSON_SERIALIZATION_FAILED);
93+
}
94+
}
95+
}
96+
Err(e) => {
97+
error!("Failed to get file content: {}", e);
98+
exit(EXIT_INVALID_INPUT);
99+
}
100+
}
64101
}
65102
None => {
66103
error!("Invalid input for file content.");
@@ -79,9 +116,21 @@ fn main() {
79116
debug!("Deleting file at path: {}", input);
80117
match parse_file(input) {
81118
Some(parsed_file) => {
82-
let file = delete_file(&parsed_file).unwrap();
83-
let json = serde_json::to_string(&file).unwrap();
84-
println!("{}", json);
119+
match delete_file(&parsed_file) {
120+
Ok(file) => {
121+
match serde_json::to_string(&file) {
122+
Ok(json) => println!("{}", json),
123+
Err(e) => {
124+
error!("Failed to serialize file: {}", e);
125+
exit(EXIT_INVALID_INPUT);
126+
}
127+
}
128+
}
129+
Err(e) => {
130+
error!("Failed to delete file: {}", e);
131+
exit(EXIT_INVALID_INPUT);
132+
}
133+
}
85134
}
86135
None => {
87136
error!("Invalid input for file.");
@@ -93,9 +142,21 @@ fn main() {
93142
debug!("Deleting directory at path: {}", input);
94143
match parse_directory(input) {
95144
Some(parsed_directory) => {
96-
let dir = delete_dir(&parsed_directory).unwrap();
97-
let json = serde_json::to_string(&dir).unwrap();
98-
println!("{}", json);
145+
match delete_dir(&parsed_directory) {
146+
Ok(dir) => {
147+
match serde_json::to_string(&dir) {
148+
Ok(json) => println!("{}", json),
149+
Err(e) => {
150+
error!("Failed to serialize directory: {}", e);
151+
exit(EXIT_INVALID_INPUT);
152+
}
153+
}
154+
}
155+
Err(e) => {
156+
error!("Failed to delete directory: {}", e);
157+
exit(EXIT_INVALID_INPUT);
158+
}
159+
}
99160
}
100161
None => {
101162
error!("Invalid input for directory.");
@@ -107,9 +168,21 @@ fn main() {
107168
debug!("Deleting file content at path: {}", input);
108169
match parse_filecontent(input) {
109170
Some(parsed_filecontent) => {
110-
let filecontent = delete_file_content(&parsed_filecontent).unwrap();
111-
let json = serde_json::to_string(&filecontent).unwrap();
112-
println!("{}", json);
171+
match delete_file_content(&parsed_filecontent) {
172+
Ok(filecontent) => {
173+
match serde_json::to_string(&filecontent) {
174+
Ok(json) => println!("{}", json),
175+
Err(e) => {
176+
error!("Failed to serialize file content: {}", e);
177+
exit(EXIT_INVALID_INPUT);
178+
}
179+
}
180+
}
181+
Err(e) => {
182+
error!("Failed to delete file content: {}", e);
183+
exit(EXIT_INVALID_INPUT);
184+
}
185+
}
113186
}
114187
None => {
115188
error!("Invalid input for file content.");
@@ -126,9 +199,21 @@ fn main() {
126199
debug!("Setting file at path: {}", input);
127200
match parse_file(input) {
128201
Some(parsed_file) => {
129-
let file = set_file(&parsed_file).unwrap();
130-
let json = serde_json::to_string(&file).unwrap();
131-
println!("{}", json);
202+
match set_file(&parsed_file) {
203+
Ok(file) => {
204+
match serde_json::to_string(&file) {
205+
Ok(json) => println!("{}", json),
206+
Err(e) => {
207+
error!("Failed to serialize file: {}", e);
208+
exit(EXIT_INVALID_INPUT);
209+
}
210+
}
211+
}
212+
Err(e) => {
213+
error!("Failed to set file: {}", e);
214+
exit(EXIT_INVALID_INPUT);
215+
}
216+
}
132217
}
133218
None => {
134219
error!("Invalid input for file.");
@@ -140,9 +225,21 @@ fn main() {
140225
debug!("Setting directory at path: {}", input);
141226
match parse_directory(input) {
142227
Some(parsed_directory) => {
143-
let dir = set_dir(&parsed_directory).unwrap();
144-
let json = serde_json::to_string(&dir).unwrap();
145-
println!("{}", json);
228+
match set_dir(&parsed_directory) {
229+
Ok(dir) => {
230+
match serde_json::to_string(&dir) {
231+
Ok(json) => println!("{}", json),
232+
Err(e) => {
233+
error!("Failed to serialize directory: {}", e);
234+
exit(EXIT_INVALID_INPUT);
235+
}
236+
}
237+
}
238+
Err(e) => {
239+
error!("Failed to set directory: {}", e);
240+
exit(EXIT_INVALID_INPUT);
241+
}
242+
}
146243
}
147244
None => {
148245
error!("Invalid input for directory.");
@@ -154,9 +251,21 @@ fn main() {
154251
debug!("Setting file content at path: {}", input);
155252
match parse_filecontent(input) {
156253
Some(parsed_filecontent) => {
157-
let filecontent = set_file_content(&parsed_filecontent).unwrap();
158-
let json = serde_json::to_string(&filecontent).unwrap();
159-
println!("{}", json);
254+
match set_file_content(&parsed_filecontent) {
255+
Ok(filecontent) => {
256+
match serde_json::to_string(&filecontent) {
257+
Ok(json) => println!("{}", json),
258+
Err(e) => {
259+
error!("Failed to serialize file content: {}", e);
260+
exit(EXIT_INVALID_INPUT);
261+
}
262+
}
263+
}
264+
Err(e) => {
265+
error!("Failed to set file content: {}", e);
266+
exit(EXIT_INVALID_INPUT);
267+
}
268+
}
160269
}
161270
None => {
162271
error!("Invalid input for file content.");
@@ -174,9 +283,21 @@ fn main() {
174283
debug!("Exporting file at path: {}", input);
175284
match parse_file(input) {
176285
Some(parsed_file) => {
177-
let file = export_file_path(&parsed_file).unwrap();
178-
let json = serde_json::to_string(&file).unwrap();
179-
println!("{}", json);
286+
match export_file_path(&parsed_file) {
287+
Ok(file) => {
288+
match serde_json::to_string(&file) {
289+
Ok(json) => println!("{}", json),
290+
Err(e) => {
291+
error!("Failed to serialize file: {}", e);
292+
exit(EXIT_INVALID_INPUT);
293+
}
294+
}
295+
}
296+
Err(e) => {
297+
error!("Failed to export file: {}", e);
298+
exit(EXIT_INVALID_INPUT);
299+
}
300+
}
180301
}
181302
None => {
182303
error!("Invalid input for file.");
@@ -188,9 +309,21 @@ fn main() {
188309
debug!("Exporting directory at path: {}", input);
189310
match parse_directory(input) {
190311
Some(parsed_directory) => {
191-
let dir = export_dir_path(&parsed_directory).unwrap();
192-
let json = serde_json::to_string(&dir).unwrap();
193-
println!("{}", json);
312+
match export_dir_path(&parsed_directory) {
313+
Ok(dir) => {
314+
match serde_json::to_string(&dir) {
315+
Ok(json) => println!("{}", json),
316+
Err(e) => {
317+
error!("Failed to serialize directory: {}", e);
318+
exit(EXIT_INVALID_INPUT);
319+
}
320+
}
321+
}
322+
Err(e) => {
323+
error!("Failed to export directory: {}", e);
324+
exit(EXIT_INVALID_INPUT);
325+
}
326+
}
194327
}
195328
None => {
196329
error!("Invalid input for directory.");
@@ -202,9 +335,21 @@ fn main() {
202335
debug!("Exporting file content at path: {}", input);
203336
match parse_filecontent(input) {
204337
Some(parsed_filecontent) => {
205-
let filecontent = get_file_content(&parsed_filecontent).unwrap();
206-
let json = serde_json::to_string(&filecontent).unwrap();
207-
println!("{}", json);
338+
match get_file_content(&parsed_filecontent) {
339+
Ok(filecontent) => {
340+
match serde_json::to_string(&filecontent) {
341+
Ok(json) => println!("{}", json),
342+
Err(e) => {
343+
error!("Failed to serialize file content: {}", e);
344+
exit(EXIT_INVALID_INPUT);
345+
}
346+
}
347+
}
348+
Err(e) => {
349+
error!("Failed to export file content: {}", e);
350+
exit(EXIT_INVALID_INPUT);
351+
}
352+
}
208353
}
209354
None => {
210355
error!("Invalid input for file content.");
@@ -218,18 +363,33 @@ fn main() {
218363
match schema_type {
219364
args::FileSystemObjectType::File => {
220365
let schema = schema_for!(File);
221-
let json = serde_json::to_string(&schema).unwrap();
222-
println!("{}", json);
366+
match serde_json::to_string(&schema) {
367+
Ok(json) => println!("{}", json),
368+
Err(e) => {
369+
error!("Failed to serialize file schema: {}", e);
370+
exit(EXIT_JSON_SERIALIZATION_FAILED);
371+
}
372+
}
223373
}
224374
args::FileSystemObjectType::Directory => {
225375
let schema = schema_for!(Directory);
226-
let json = serde_json::to_string(&schema).unwrap();
227-
println!("{}", json);
376+
match serde_json::to_string(&schema){
377+
Ok(json) => println!("{}", json),
378+
Err(e) => {
379+
error!("Failed to serialize directory schema: {}", e);
380+
exit(EXIT_JSON_SERIALIZATION_FAILED);
381+
}
382+
}
228383
}
229384
args::FileSystemObjectType::FileContent => {
230385
let schema = schema_for!(FileContent);
231-
let json = serde_json::to_string(&schema).unwrap();
232-
println!("{}", json);
386+
match serde_json::to_string(&schema) {
387+
Ok(json) => println!("{}", json),
388+
Err(e) => {
389+
error!("Failed to serialize file content schema: {}", e);
390+
exit(EXIT_JSON_SERIALIZATION_FAILED);
391+
}
392+
}
233393
}
234394
}
235395
}

0 commit comments

Comments
 (0)