/
TestImpactTestSelectorAndPrioritizer.h
763 lines (700 loc) · 37.6 KB
/
TestImpactTestSelectorAndPrioritizer.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include <BuildTarget/Common/TestImpactBuildGraph.h>
#include <TestImpactFramework/TestImpactPolicy.h>
#include <Dependency/TestImpactChangeDependencyList.h>
#include <AzCore/std/containers/set.h>
#include <AzCore/std/containers/unordered_map.h>
#include <AzCore/std/containers/unordered_set.h>
#include <AzCore/std/containers/vector.h>
namespace TestImpact
{
template<typename ProductionTarget, typename TestTarget>
class DynamicDependencyMap;
class Target;
//! Selects the test targets that cover a given set of changes based on the CRUD rules and optionally prioritizes the test
//! selection according to their locality of their covering production targets in the their dependency graphs.
//! @note the CRUD rules for how tests are selected can be found in the MicroRepo header file.
template<typename ProductionTarget, typename TestTarget>
class TestSelectorAndPrioritizer
{
public:
//! Constructs the test selector and prioritizer for the given dynamic dependency map.
//! @param dynamicDependencyMap The dynamic dependency map representing the repository source tree.
TestSelectorAndPrioritizer(
const DynamicDependencyMap<ProductionTarget, TestTarget>& dynamicDependencyMap);
virtual ~TestSelectorAndPrioritizer() = default;
//! Select the covering test targets for the given set of source changes and optionally prioritizes said test selection.
//! @param changeDependencyList The resolved list of source dependencies for the CRUD source changes.
//! @param testSelectionStrategy The test selection and prioritization strategy to apply to the given CRUD source changes.
AZStd::vector<const TestTarget*> SelectTestTargets(
const ChangeDependencyList<ProductionTarget, TestTarget>& changeDependencyList, Policy::TestPrioritization testSelectionStrategy);
protected:
//! Map of selected test targets and the production targets they cover for the given set of source changes.
using SelectedTestTargetAndDependerMap = AZStd::unordered_map<const TestTarget*, AZStd::unordered_set<const ProductionTarget*>>;
private:
//! Selects the test targets covering the set of source changes in the change dependency list.
//! @param changeDependencyList The change dependency list containing the CRUD source changes to select tests for.
//! @returns The selected tests and their covering production targets for the given set of source changes.
SelectedTestTargetAndDependerMap SelectTestTargets(const ChangeDependencyList<ProductionTarget, TestTarget>& changeDependencyList);
//! Prioritizes the selected tests according to the specified test selection strategy,
//! @param selectedTestTargetAndDependerMap The selected tests to prioritize.
//! @param testSelectionStrategy The test selection strategy to prioritize the selected tests.
//! @returns The selected tests either in either arbitrary order or in prioritized with highest priority first.
AZStd::vector<const TestTarget*> PrioritizeSelectedTestTargets(
const SelectedTestTargetAndDependerMap& selectedTestTargetAndDependerMap, Policy::TestPrioritization testSelectionStrategy);
protected:
//! Result of source dependency action.
enum class SourceOperationActionResult : AZ::u8
{
Continue, //!< Continue with actions for this CRUD operation.
ConcludeSelection, //!< Conclude selection (no further actions).
};
//! Iterate over the production and test targets for newly created sources with no coverage and act on them.
[[nodiscard]] virtual SourceOperationActionResult IterateCreateParentedSourcesWithNoCoverage(
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency,
SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Iterate over the production and test targets for updated sources with coverage and act on them.
[[nodiscard]] virtual SourceOperationActionResult IterateUpdateParentedSourcesWithCoverage(
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency,
SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Iterate over the production and test targets for updated sources with no coverage and act on them.
[[nodiscard]] virtual SourceOperationActionResult IterateUpdateParentedSourcesWithoutCoverage(
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency,
SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Action to perform when production sources are created.
[[nodiscard]] virtual SourceOperationActionResult CreateProductionSourceWithoutCoverageAction(
const ProductionTarget* target, SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Action to perform when test sources are created.
[[nodiscard]] virtual SourceOperationActionResult CreateTestSourceCreateProductionSourceWithoutCoverageAction(
const TestTarget* target, SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Action to perform when production sources with coverage are updated.
[[nodiscard]] virtual SourceOperationActionResult UpdateProductionSourceWithCoverageAction(
const ProductionTarget* target,
SelectedTestTargetAndDependerMap& selectedTestTargetMap,
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency);
//! Action to perform when test sources with coverage are updated.
[[nodiscard]] virtual SourceOperationActionResult UpdateTestSourceWithCoverageAction(
const TestTarget* target,
SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Action to perform when production sources without coverage are updated.
[[nodiscard]] virtual SourceOperationActionResult UpdateProductionSourceWithoutCoverageAction(
const ProductionTarget* target,
SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Action to perform when test sources without coverage are updated.
[[nodiscard]] virtual SourceOperationActionResult UpdateTestSourceWithoutCoverageAction(
const TestTarget* target,
SelectedTestTargetAndDependerMap& selectedTestTargetMap);
//! Action to perform when sources that cannot be determined to be production or test sources with coverage are updated.
[[nodiscard]] virtual SourceOperationActionResult UpdateIndeterminateSourceWithCoverageAction(
SelectedTestTargetAndDependerMap& selectedTestTargetMap,
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency);
//! Action to perform when sources that cannot be determined to be production or test sources with coverage are deleted.
[[nodiscard]] virtual SourceOperationActionResult DeleteIndeterminateSourceWithCoverageAction(
SelectedTestTargetAndDependerMap& selectedTestTargetMap, const SourceDependency<ProductionTarget, TestTarget>& sourceDependency);
const DynamicDependencyMap<ProductionTarget, TestTarget>* m_dynamicDependencyMap;
};
template<typename ProductionTarget, typename TestTarget>
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::TestSelectorAndPrioritizer(
const DynamicDependencyMap<ProductionTarget, TestTarget>& dynamicDependencyMap)
: m_dynamicDependencyMap(&dynamicDependencyMap)
{
}
template<typename ProductionTarget, typename TestTarget>
AZStd::vector<const TestTarget*> TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SelectTestTargets(
const ChangeDependencyList<ProductionTarget, TestTarget>& changeDependencyList, Policy::TestPrioritization testSelectionStrategy)
{
const auto selectedTestTargetAndDependerMap = SelectTestTargets(changeDependencyList);
const auto prioritizedSelectedTests = PrioritizeSelectedTestTargets(selectedTestTargetAndDependerMap, testSelectionStrategy);
return prioritizedSelectedTests;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::IterateCreateParentedSourcesWithNoCoverage(
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency,
SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
for (const auto& parentTarget : sourceDependency.GetParentTargets())
{
if (parentTarget.GetTarget()->GetType() == TargetType::ProductionTarget)
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Production
//
// Scenario
// 1. The file has been newly created
// 2. This file exists in one or more source to production target mapping artifacts
// 3. There exists no coverage data for this file in the source covering test list
if (SourceOperationActionResult::ConcludeSelection ==
CreateProductionSourceWithoutCoverageAction(parentTarget.GetProductionTarget(), selectedTestTargetMap))
{
return SourceOperationActionResult::ConcludeSelection;
}
}
else
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Test
//
// Scenario
// 1. The file has been newly created
// 2. This file exists in one or more source to test target mapping artifacts
// 3. There exists no coverage data for this file in the source covering test list
if (SourceOperationActionResult::ConcludeSelection ==
CreateTestSourceCreateProductionSourceWithoutCoverageAction(parentTarget.GetTestTarget(), selectedTestTargetMap))
{
return SourceOperationActionResult::ConcludeSelection;
}
}
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::IterateUpdateParentedSourcesWithCoverage(
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency,
SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
for (const auto& parentTarget : sourceDependency.GetParentTargets())
{
if (parentTarget.GetTarget()->GetType() == TargetType::ProductionTarget)
{
// Parent Targets: Yes
// Coverage Data : Yes
// Source Type : Production
//
// Scenario
// 1. The existing file has been modified
// 2. This file exists in one or more source to production target mapping artifacts
// 3. There exists coverage data for this file in the source covering test list
if (SourceOperationActionResult::ConcludeSelection ==
UpdateProductionSourceWithCoverageAction(parentTarget.GetProductionTarget(), selectedTestTargetMap, sourceDependency))
{
return SourceOperationActionResult::ConcludeSelection;
}
}
else
{
// Parent Targets: Yes
// Coverage Data : Yes
// Source Type : Test
//
// Scenario
// 1. The existing file has been modified
// 2. This file exists in one or more source to test target mapping artifacts
// 3. There exists coverage data for this file in the source covering test list
if (SourceOperationActionResult::ConcludeSelection ==
UpdateTestSourceWithCoverageAction(parentTarget.GetTestTarget(), selectedTestTargetMap))
{
return SourceOperationActionResult::ConcludeSelection;
}
}
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::IterateUpdateParentedSourcesWithoutCoverage(
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency,
SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
for (const auto& parentTarget : sourceDependency.GetParentTargets())
{
if (parentTarget.GetTarget()->GetType() == TargetType::ProductionTarget)
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Production
//
// Scenario
// 1. The existing file has been modified
// 2. This file exists in one or more source to test target mapping artifacts
// 3. There exists no coverage data for this file in the source covering test list
if (SourceOperationActionResult::ConcludeSelection ==
UpdateProductionSourceWithoutCoverageAction(parentTarget.GetProductionTarget(), selectedTestTargetMap))
{
return SourceOperationActionResult::ConcludeSelection;
}
}
else
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Test
//
// Scenario
// 1. The existing file has been modified
// 2. This file exists in one or more source to test target mapping artifacts
// 3. There exists no coverage data for this file in the source covering test list
if (SourceOperationActionResult::ConcludeSelection ==
UpdateTestSourceWithoutCoverageAction(parentTarget.GetTestTarget(), selectedTestTargetMap))
{
return SourceOperationActionResult::ConcludeSelection;
}
}
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::CreateProductionSourceWithoutCoverageAction(
const ProductionTarget* target, SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
// Action
// 1. Select all test targets covering the parent production targets
const auto coverage = m_dynamicDependencyMap->GetCoveringTestTargetsForProductionTarget(*target);
for (const auto* testTarget : coverage)
{
selectedTestTargetMap[testTarget].insert(target);
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::CreateTestSourceCreateProductionSourceWithoutCoverageAction(
const TestTarget* target, SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
// Action
// 1. Select all parent test targets
selectedTestTargetMap.insert(target);
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::
UpdateProductionSourceWithCoverageAction(
const ProductionTarget* target,
SelectedTestTargetAndDependerMap& selectedTestTargetMap,
const SourceDependency<ProductionTarget, TestTarget>& sourceDependency)
{
// Action
// 1. Select all test targets covering this file
for (const auto* testTarget : sourceDependency.GetCoveringTestTargets())
{
selectedTestTargetMap[testTarget].insert(target);
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::
UpdateTestSourceWithCoverageAction(
const TestTarget* target, SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
// Action
// 1. Select the parent test targets for this file
selectedTestTargetMap.insert(target);
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::
UpdateProductionSourceWithoutCoverageAction(
[[maybe_unused]] const ProductionTarget* target,
[[maybe_unused]] SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
// Action
// 1. Skip the file
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::
UpdateTestSourceWithoutCoverageAction(
const TestTarget* target, SelectedTestTargetAndDependerMap& selectedTestTargetMap)
{
// Action
// 1. Select the parent test targets for this file
selectedTestTargetMap.insert(target);
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::
UpdateIndeterminateSourceWithCoverageAction(
SelectedTestTargetAndDependerMap& selectedTestTargetMap, const SourceDependency<ProductionTarget, TestTarget>& sourceDependency)
{
// Action
// 1. Log potential orphaned source file warning (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 2. Select all test targets covering this file
// 3. Delete the existing coverage data from the source covering test list (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
for (const auto* testTarget : sourceDependency.GetCoveringTestTargets())
{
selectedTestTargetMap.insert(testTarget);
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SourceOperationActionResult
TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::DeleteIndeterminateSourceWithCoverageAction(
SelectedTestTargetAndDependerMap& selectedTestTargetMap, const SourceDependency<ProductionTarget, TestTarget>& sourceDependency)
{
// Action
// 1. Select all test targets covering this file
// 2. Delete the existing coverage data from the source covering test list (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
for (const auto* testTarget : sourceDependency.GetCoveringTestTargets())
{
selectedTestTargetMap.insert(testTarget);
}
return SourceOperationActionResult::Continue;
}
template<typename ProductionTarget, typename TestTarget>
typename TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SelectedTestTargetAndDependerMap TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::SelectTestTargets(
const ChangeDependencyList<ProductionTarget, TestTarget>& changeDependencyList)
{
SelectedTestTargetAndDependerMap selectedTestTargetMap;
// Create operations
for (const auto& sourceDependency : changeDependencyList.GetCreateSourceDependencies())
{
if (sourceDependency.GetNumParentTargets())
{
if (sourceDependency.GetNumCoveringTestTargets())
{
// Parent Targets: Yes
// Coverage Data : Yes
// Source Type : Irrelevant
//
// Scenario
// 1. This file previously existed in one or more source to target mapping artifacts
// 2. The file has since been deleted yet no delete crud operation acted upon:
// a) The coverage data for this file was not deleted from the Source Covering Test List
// 3. The file has since been recreated
// 4. This file exists in one or more source to target mapping artifacts
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Log Source Covering Test List compromised error
// 2. Throw exception
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
AZStd::string::format(
"Create operation for file with parent targets and coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str())
.c_str());
continue;
}
else
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Production or test
if (SourceOperationActionResult::ConcludeSelection ==
IterateCreateParentedSourcesWithNoCoverage(sourceDependency, selectedTestTargetMap))
{
return selectedTestTargetMap;
}
}
}
else
{
if (sourceDependency.GetNumCoveringTestTargets())
{
// Parent Targets: No
// Coverage Data : Yes
// Source Type : Indeterminate
//
// Scenario
// 1. This file previously existed in one or more source to target mapping artifacts
// 2. The file has since been deleted yet no delete crud operation acted upon:
// a) The coverage data for this file was not deleted from the Source Covering Test List
// 3. The file has since been recreated
// 4. This file does not exist in any source to target mapping artifacts
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Log Source Covering Test List compromised error
// 2. Throw exception
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
"Create operation for file with no parent targets but coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str());
continue;
}
else
{
// Parent Targets: No
// Coverage Data : No
// Source Type : Irrelevant
//
// Scenario
// 1. The file has been newly created
// 2. This file does not exists in any source to target mapping artifacts.
//
// Action
// 1. Skip the file
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
"Create operation for file with no parent targets or coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str());
continue;
}
}
}
// Update operations
for (const auto& sourceDependency : changeDependencyList.GetUpdateSourceDependencies())
{
if (sourceDependency.GetNumParentTargets())
{
if (sourceDependency.GetNumCoveringTestTargets())
{
// Parent Targets: Yes
// Coverage Data : Yes
// Source Type : Production or test
if (SourceOperationActionResult::ConcludeSelection ==
IterateUpdateParentedSourcesWithCoverage(sourceDependency, selectedTestTargetMap))
{
return selectedTestTargetMap;
}
}
else
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Production or test
if (SourceOperationActionResult::ConcludeSelection ==
IterateUpdateParentedSourcesWithoutCoverage(sourceDependency, selectedTestTargetMap))
{
return selectedTestTargetMap;
}
}
}
else
{
if (sourceDependency.GetNumCoveringTestTargets())
{
// Parent Targets: No
// Coverage Data : Yes
// Source Type : Indeterminate
//
// Scenario
// 1. The existing file has been modified
// 2. Either:
// a) This file previously existed in one or more source to target mapping artifacts
// b) This file no longer exists in any source to target mapping artifacts
// c) The coverage data for this file was has yet to be deleted from the source covering test list
// 3. Or:
// a) The file is being used by build targets but has erroneously not been explicitly added to the build
// system (e.g. include directive pulling in a header from the repository that has not been added to
// any build targets due to an oversight)
if (SourceOperationActionResult::ConcludeSelection ==
UpdateIndeterminateSourceWithCoverageAction(selectedTestTargetMap, sourceDependency))
{
return selectedTestTargetMap;
}
}
else
{
// Parent Targets: No
// Coverage Data : No
// Source Type : Indeterminate
//
// Scenario
// 1. The existing file has been modified
// 2. This file does not exist in any source to target mapping artifacts
// 3. There exists no coverage data for this file in the Source Covering Test List
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Skip the file
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
"Update operation for file with no parent targets or coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str());
continue;
}
}
}
// Delete operations
for (const auto& sourceDependency : changeDependencyList.GetDeleteSourceDependencies())
{
if (sourceDependency.GetNumParentTargets())
{
if (sourceDependency.GetNumCoveringTestTargets())
{
// Parent Targets: Yes
// Coverage Data : Yes
// Source Type : Irrelevant
//
// Scenario
// 1. The file has been deleted.
// 2. This file still exists in one or more source to target mapping artifacts
// 2. There exists coverage data for this file in the Source Covering Test List
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Log source to target mapping and Source Covering Test List integrity compromised error
// 2. Throw exception
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
"Delete operation for file with parent targets and coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str());
continue;
}
else
{
// Parent Targets: Yes
// Coverage Data : No
// Source Type : Irrelevant
//
// Scenario
// 1. The file has been deleted.
// 2. This file still exists in one or more source to target mapping artifacts
// 2. There exists no coverage data for this file in the Source Covering Test List
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Log source to target mapping and Source Covering Test List integrity compromised error
// 2. Throw exception
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
"Delete operation for file with parent targets but no coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str());
continue;
}
}
else
{
if (sourceDependency.GetNumCoveringTestTargets())
{
// Parent Targets: No
// Coverage Data : Yes
// Source Type : Indeterminate
//
// Scenario
// 1. The existing file has been deleted
// 2. This file previously existed in one or more source to target mapping artifacts
// 2. This file does not exist in any source to target mapping artifacts
// 4. The coverage data for this file was has yet to be deleted from the source covering test list
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Delete the existing coverage data from the Source Covering Test List
// 2. Skip the file
if (SourceOperationActionResult::ConcludeSelection ==
DeleteIndeterminateSourceWithCoverageAction(selectedTestTargetMap, sourceDependency))
{
return selectedTestTargetMap;
}
}
else
{
// Parent Targets: No
// Coverage Data : No
// Source Type : Indeterminate
//
// Scenario
// 1. The file has been deleted
// 2. This file does not exist in any source to target mapping artifacts
// 3. There exists no coverage data for this file in the Source Covering Test List
//
// Action (handled prior by DynamicDependencyMap::ApplyAndResoveChangeList)
// 1. Skip the file
AZ_Warning(
"TestSelectorAndPrioritizer",
false,
AZStd::string::format(
"Delete operation for file with no parent targets or coverage data was not expected to be handled here for "
"file '%s'",
sourceDependency.GetPath().c_str())
.c_str());
continue;
}
}
}
return selectedTestTargetMap;
}
template<typename ProductionTarget, typename TestTarget>
AZStd::vector<const TestTarget*> TestSelectorAndPrioritizer<ProductionTarget, TestTarget>::PrioritizeSelectedTestTargets(
const SelectedTestTargetAndDependerMap& selectedTestTargetAndDependerMap, const Policy::TestPrioritization testSelectionStrategy)
{
AZStd::vector<const TestTarget*> selectedTestTargets;
selectedTestTargets.reserve(selectedTestTargetAndDependerMap.size());
const auto fillSelectedTestTargets = [&selectedTestTargets](const auto& containerOfPairs)
{
AZStd::transform(
containerOfPairs.begin(),
containerOfPairs.end(),
AZStd::back_inserter(selectedTestTargets),
[](const auto& pair)
{
return pair.first;
});
};
if (testSelectionStrategy == Policy::TestPrioritization::DependencyLocality)
{
const BuildTargetList<ProductionTarget, TestTarget>* buildTargetList = m_dynamicDependencyMap->GetBuildTargetList();
const BuildGraph<ProductionTarget, TestTarget>& buildGraph = buildTargetList->GetBuildGraph();
AZStd::vector<AZStd::pair<const TestTarget*, AZStd::optional<AZStd::size_t>>> testTargetDistancePairs;
testTargetDistancePairs.reserve(selectedTestTargetAndDependerMap.size());
// Loop through each test target in our map, walk the build dependency graph for that target and retrieve the vertices for the
// production targets in productionTargets. Find and store the distance to the closest productionTarget if one exists.
for (const auto [testTarget, productionTargets] : selectedTestTargetAndDependerMap)
{
AZStd::optional<AZStd::size_t> minimum;
buildGraph.WalkBuildDependencies(
buildTargetList->GetBuildTargetOrThrow(testTarget->GetName()),
// Workaround for C++ Standard bug where structured bindings names aren't capturable in lambdas
// https://stackoverflow.com/a/46115028
[&, productionTargets = productionTargets](const BuildGraphVertex<ProductionTarget, TestTarget>& vertex, AZStd::size_t distance)
{
if (const auto productionTarget = vertex.m_buildTarget.GetProductionTarget();
productionTarget && productionTargets.contains(productionTarget))
{
if (!minimum.has_value() || distance < minimum)
{
minimum = distance;
};
}
return BuildGraphVertexVisitResult::Continue;
});
// If we found any productionTargets, store the distance to the closest one.
// Else we didn't find any vertices, store the testTarget with no distance. This will be interpreted as infinite distance
// and the test targets priority will be lower
if (minimum.has_value())
{
testTargetDistancePairs.emplace_back(testTarget, minimum);
}
else
{
testTargetDistancePairs.emplace_back(testTarget);
}
}
// Sort our pairs by distance and put our test targets into the vector in order of distance (closest first)
AZStd::sort(
testTargetDistancePairs.begin(),
testTargetDistancePairs.end(),
[](const auto& left, const auto& right)
{
if (!left.second.has_value())
{
return false;
}
else if (!right.second.has_value())
{
return true;
}
return left.second.value() < right.second.value();
});
fillSelectedTestTargets(testTargetDistancePairs);
}
else
{
fillSelectedTestTargets(selectedTestTargetAndDependerMap);
}
return selectedTestTargets;
}
} // namespace TestImpact