Skip to content

Commit a087de5

Browse files
committed
Fix ordering of setting progress data and writing it
1 parent 93e52da commit a087de5

File tree

4 files changed

+41
-56
lines changed

4 files changed

+41
-56
lines changed

dsc/tests/dsc_config_get.tests.ps1

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,21 @@ Describe 'dsc config get tests' {
6060
$config_yaml = @"
6161
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
6262
resources:
63-
- name: Echo
63+
- name: Echo 1
6464
type: Microsoft.DSC.Debug/Echo
6565
properties:
6666
output: hello
67+
- name: Echo 2
68+
type: Microsoft.DSC.Debug/Echo
69+
properties:
70+
output: world
6771
"@
6872
$config_yaml | dsc --progress-format json config get -f - 2> $TestDrive/ErrorStream.txt
6973
$LASTEXITCODE | Should -Be 0
7074
$lines = Get-Content $TestDrive/ErrorStream.txt
7175
$ProgressMessagesFound = $false
72-
$ProgressResultFound = $false
76+
$InstanceOneFound = $false
77+
$InstanceTwoFound = $false
7378
foreach ($line in $lines) {
7479
$jp = $line | ConvertFrom-Json
7580
if ($jp.activity) { # if line is a progress message
@@ -78,15 +83,19 @@ Describe 'dsc config get tests' {
7883
$ProgressMessagesFound = $true
7984
}
8085

81-
if ($jp.percentComplete -eq 100 -and $jp.resourceType -eq 'Microsoft.DSC.Debug/Echo') {
82-
$ProgressResultFound = $true
83-
$jp.resourceName | Should -BeExactly 'Echo'
84-
$jp.result | Should -Not -BeNullOrEmpty
85-
$jp.result.output | Should -BeExactly 'hello'
86+
if ($null -ne $jp.result -and $jp.resourceType -eq 'Microsoft.DSC.Debug/Echo') {
87+
if ($jp.resourceName -eq 'Echo 1') {
88+
$InstanceOneFound = $true
89+
$jp.result.actualState.output | Should -BeExactly 'hello'
90+
} elseif ($jp.resourceName -eq 'Echo 2') {
91+
$InstanceTwoFound = $true
92+
$jp.result.actualState.output | Should -BeExactly 'world'
93+
}
8694
}
8795
}
8896
$ProgressMessagesFound | Should -BeTrue
89-
$ProgressResultFound | Should -BeTrue
97+
$InstanceOneFound | Should -BeTrue
98+
$InstanceTwoFound | Should -BeTrue
9099
}
91100

92101
It 'contentVersion is ignored' {

dsc_lib/src/configure/mod.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ impl Configurator {
232232
let resources = get_resource_invocation_order(&self.config, &mut self.statement_parser, &self.context)?;
233233
let mut progress = ProgressBar::new(resources.len() as u64, self.progress_format)?;
234234
for resource in resources {
235-
progress.set_activity(format!("Get '{}'", resource.name).as_str());
236235
progress.set_resource(&resource.name, &resource.resource_type, None);
236+
progress.write_activity(format!("Get '{}'", resource.name).as_str());
237237
let properties = self.invoke_property_expressions(resource.properties.as_ref())?;
238238
let Some(dsc_resource) = self.discovery.find_resource(&resource.resource_type) else {
239239
return Err(DscError::ResourceNotFound(resource.resource_type));
@@ -247,15 +247,13 @@ impl Configurator {
247247
match &get_result {
248248
GetResult::Resource(resource_result) => {
249249
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), serde_json::to_value(&resource_result.actual_state)?);
250-
progress.set_resource(&resource.name, &resource.resource_type, Some(&resource_result.actual_state));
251250
},
252251
GetResult::Group(group) => {
253252
let mut results = Vec::<Value>::new();
254253
for result in group {
255254
results.push(serde_json::to_value(&result.result)?);
256255
}
257256
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), Value::Array(results.clone()));
258-
progress.set_resource(&resource.name, &resource.resource_type, Some(&Value::Array(results.clone())));
259257
},
260258
}
261259
let resource_result = config_result::ResourceGetResult {
@@ -271,10 +269,11 @@ impl Configurator {
271269
),
272270
name: resource.name.clone(),
273271
resource_type: resource.resource_type.clone(),
274-
result: get_result,
272+
result: get_result.clone(),
275273
};
276274
result.results.push(resource_result);
277-
progress.increment(1);
275+
progress.set_resource(&resource.name, &resource.resource_type, Some(&serde_json::to_value(get_result)?));
276+
progress.write_increment(1);
278277
}
279278

280279
result.metadata = Some(
@@ -301,8 +300,8 @@ impl Configurator {
301300
let resources = get_resource_invocation_order(&self.config, &mut self.statement_parser, &self.context)?;
302301
let mut progress = ProgressBar::new(resources.len() as u64, self.progress_format)?;
303302
for resource in resources {
304-
progress.set_activity(format!("Set '{}'", resource.name).as_str());
305303
progress.set_resource(&resource.name, &resource.resource_type, None);
304+
progress.write_activity(format!("Set '{}'", resource.name).as_str());
306305
let properties = self.invoke_property_expressions(resource.properties.as_ref())?;
307306
let Some(dsc_resource) = self.discovery.find_resource(&resource.resource_type) else {
308307
return Err(DscError::ResourceNotFound(resource.resource_type));
@@ -370,18 +369,15 @@ impl Configurator {
370369
match &set_result {
371370
SetResult::Resource(resource_result) => {
372371
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), serde_json::to_value(&resource_result.after_state)?);
373-
progress.set_resource(&resource.name, &resource.resource_type, Some(&resource_result.after_state));
374372
},
375373
SetResult::Group(group) => {
376374
let mut results = Vec::<Value>::new();
377375
for result in group {
378376
results.push(serde_json::to_value(&result.result)?);
379377
}
380378
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), Value::Array(results.clone()));
381-
progress.set_resource(&resource.name, &resource.resource_type, Some(&Value::Array(results.clone())));
382379
},
383380
}
384-
385381
let resource_result = config_result::ResourceSetResult {
386382
metadata: Some(
387383
Metadata {
@@ -395,10 +391,11 @@ impl Configurator {
395391
),
396392
name: resource.name.clone(),
397393
resource_type: resource.resource_type.clone(),
398-
result: set_result,
394+
result: set_result.clone(),
399395
};
400396
result.results.push(resource_result);
401-
progress.increment(1);
397+
progress.set_resource(&resource.name, &resource.resource_type, Some(&serde_json::to_value(set_result)?));
398+
progress.write_increment(1);
402399
}
403400

404401
result.metadata = Some(
@@ -421,8 +418,8 @@ impl Configurator {
421418
let resources = get_resource_invocation_order(&self.config, &mut self.statement_parser, &self.context)?;
422419
let mut progress = ProgressBar::new(resources.len() as u64, self.progress_format)?;
423420
for resource in resources {
424-
progress.set_activity(format!("Test '{}'", resource.name).as_str());
425421
progress.set_resource(&resource.name, &resource.resource_type, None);
422+
progress.write_activity(format!("Test '{}'", resource.name).as_str());
426423
let properties = self.invoke_property_expressions(resource.properties.as_ref())?;
427424
let Some(dsc_resource) = self.discovery.find_resource(&resource.resource_type) else {
428425
return Err(DscError::ResourceNotFound(resource.resource_type));
@@ -436,15 +433,13 @@ impl Configurator {
436433
match &test_result {
437434
TestResult::Resource(resource_test_result) => {
438435
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), serde_json::to_value(&resource_test_result.actual_state)?);
439-
progress.set_resource(&resource.name, &resource.resource_type, Some(&resource_test_result.actual_state));
440436
},
441437
TestResult::Group(group) => {
442438
let mut results = Vec::<Value>::new();
443439
for result in group {
444440
results.push(serde_json::to_value(&result.result)?);
445441
}
446442
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), Value::Array(results.clone()));
447-
progress.set_resource(&resource.name, &resource.resource_type, Some(&Value::Array(results.clone())));
448443
},
449444
}
450445
let resource_result = config_result::ResourceTestResult {
@@ -460,10 +455,11 @@ impl Configurator {
460455
),
461456
name: resource.name.clone(),
462457
resource_type: resource.resource_type.clone(),
463-
result: test_result,
458+
result: test_result.clone(),
464459
};
465460
result.results.push(resource_result);
466-
progress.increment(1);
461+
progress.set_resource(&resource.name, &resource.resource_type, Some(&serde_json::to_value(test_result)?));
462+
progress.write_increment(1);
467463
}
468464

469465
result.metadata = Some(
@@ -488,8 +484,8 @@ impl Configurator {
488484
let mut progress = ProgressBar::new(self.config.resources.len() as u64, self.progress_format)?;
489485
let resources = self.config.resources.clone();
490486
for resource in &resources {
491-
progress.set_activity(format!("Export '{}'", resource.name).as_str());
492487
progress.set_resource(&resource.name, &resource.resource_type, None);
488+
progress.write_activity(format!("Export '{}'", resource.name).as_str());
493489
let properties = self.invoke_property_expressions(resource.properties.as_ref())?;
494490
let Some(dsc_resource) = self.discovery.find_resource(&resource.resource_type) else {
495491
return Err(DscError::ResourceNotFound(resource.resource_type.clone()));
@@ -498,8 +494,8 @@ impl Configurator {
498494
trace!("{}", t!("configure.mod.exportInput", input = input));
499495
let export_result = add_resource_export_results_to_configuration(dsc_resource, Some(dsc_resource), &mut conf, input.as_str())?;
500496
self.context.references.insert(format!("{}:{}", resource.resource_type, resource.name), serde_json::to_value(&export_result.actual_state)?);
501-
progress.set_resource(&resource.name, &resource.resource_type, Some(&Value::Array(export_result.actual_state)));
502-
progress.increment(1);
497+
progress.set_resource(&resource.name, &resource.resource_type, Some(&serde_json::to_value(export_result)?));
498+
progress.write_increment(1);
503499
}
504500

505501
conf.metadata = Some(self.get_result_metadata(Operation::Export));

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl ResourceDiscovery for CommandDiscovery {
179179
};
180180

181181
let mut progress = ProgressBar::new(1, progress_format)?;
182-
progress.set_activity(t!("discovery.commandDiscovery.progressSearching").to_string().as_str());
182+
progress.write_activity(t!("discovery.commandDiscovery.progressSearching").to_string().as_str());
183183

184184
let mut resources = BTreeMap::<String, Vec<DscResource>>::new();
185185
let mut adapters = BTreeMap::<String, Vec<DscResource>>::new();
@@ -235,7 +235,7 @@ impl ResourceDiscovery for CommandDiscovery {
235235
}
236236
}
237237
}
238-
progress.increment(1);
238+
progress.write_increment(1);
239239
debug!("Found {} matching non-adapter-based resources", resources.len());
240240
self.resources = resources;
241241
self.adapters = adapters;
@@ -268,14 +268,14 @@ impl ResourceDiscovery for CommandDiscovery {
268268
};
269269

270270
let mut progress = ProgressBar::new(self.adapters.len() as u64, progress_format)?;
271-
progress.set_activity("Searching for adapted resources");
271+
progress.write_activity("Searching for adapted resources");
272272

273273
let mut adapted_resources = BTreeMap::<String, Vec<DscResource>>::new();
274274

275275
let mut found_adapter: bool = false;
276276
for (adapter_name, adapters) in &self.adapters {
277277
for adapter in adapters {
278-
progress.increment(1);
278+
progress.write_increment(1);
279279

280280
if !regex.is_match(adapter_name) {
281281
continue;
@@ -284,7 +284,7 @@ impl ResourceDiscovery for CommandDiscovery {
284284
found_adapter = true;
285285
info!("Enumerating resources for adapter '{}'", adapter_name);
286286
let mut adapter_progress = ProgressBar::new(1, progress_format)?;
287-
adapter_progress.set_activity(format!("Enumerating resources for adapter '{adapter_name}'").as_str());
287+
adapter_progress.write_activity(format!("Enumerating resources for adapter '{adapter_name}'").as_str());
288288
let manifest = if let Some(manifest) = &adapter.manifest {
289289
if let Ok(manifest) = import_manifest(manifest.clone()) {
290290
manifest
@@ -336,7 +336,7 @@ impl ResourceDiscovery for CommandDiscovery {
336336
};
337337
}
338338

339-
adapter_progress.increment(1);
339+
adapter_progress.write_increment(1);
340340
debug!("Adapter '{}' listed {} resources", adapter_name, adapter_resources_count);
341341
}
342342
}

dsc_lib/src/progress.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl ProgressBar {
109109
///
110110
/// * `delta` - The amount to increment the progress bar by
111111
///
112-
pub fn increment(&mut self, delta: u64) {
112+
pub fn write_increment(&mut self, delta: u64) {
113113
if self.format == ProgressFormat::None {
114114
return;
115115
}
@@ -131,7 +131,7 @@ impl ProgressBar {
131131
}
132132
}
133133

134-
/// Set the resource being operated on and write the progress
134+
/// Set the resource being operated on
135135
///
136136
/// # Arguments
137137
///
@@ -151,7 +151,7 @@ impl ProgressBar {
151151
///
152152
/// * `status` - The status of the operation
153153
///
154-
pub fn set_activity(&mut self, activity: &str) {
154+
pub fn write_activity(&mut self, activity: &str) {
155155
match self.format {
156156
ProgressFormat::Json => {
157157
self.progress_value.activity = Some(activity.to_string());
@@ -183,26 +183,6 @@ impl ProgressBar {
183183
}
184184
}
185185

186-
/// Set the position as progress through the items and write the progress
187-
///
188-
/// # Arguments
189-
///
190-
/// * `pos` - The position as progress through the items
191-
///
192-
pub fn set_position(&mut self, pos: u64) {
193-
match self.format {
194-
ProgressFormat::Json => {
195-
self.item_position = pos;
196-
self.set_percent_complete();
197-
self.write_json();
198-
},
199-
ProgressFormat::Default => {
200-
self.console_bar.pb_set_position(pos);
201-
},
202-
ProgressFormat::None => {}
203-
}
204-
}
205-
206186
fn write_json(&self) {
207187
if let Ok(json) = serde_json::to_string(&self.progress_value) {
208188
eprintln!("{json}");

0 commit comments

Comments
 (0)