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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
// SPDX-FileCopyrightText: 2024 Mathieu Fenniak <mathieu@fenniak.net>
//
// SPDX-License-Identifier: GPL-3.0-or-later
use anyhow::{Context as _, Result, anyhow};
use gomod_rs::{Directive, parse_gomod};
use log::{debug, error, info, trace, warn};
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::fs::{File, read_to_string};
use std::hash::Hash;
use std::io::{BufRead as _, BufReader};
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock};
use std::{fmt, fs, io};
use tempfile::TempDir;
use tokio::process::Command;
use tracing::{Instrument as _, info_span, instrument};
use crate::cmd::ui::UiStage;
use crate::coverage::Tag;
use crate::coverage::commit_coverage_data::{
CommitCoverageData, CoverageIdentifier, FileCoverage, FileReference, HeuristicCoverage,
};
use crate::coverage::full_coverage_data::FullCoverageData;
use crate::errors::{
FailedTestResult, RunTestError, RunTestsErrors, SubcommandErrors, TestFailure,
};
use crate::network::NetworkDependency;
use crate::platform::util::normalize_path;
use crate::scm::{Scm, ScmCommit};
use crate::sys_trace::trace::{ResolvedSocketAddr, Trace};
use crate::sys_trace::{SYS_TRACE_COMMAND, SysTraceCommand as _};
use super::util::spawn_limited_concurrency;
use super::{
ConcreteTestIdentifier, PlatformSpecificRelevantTestCaseData, TestDiscovery, TestIdentifier,
TestIdentifierCore, TestPlatform, TestReason,
};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct GolangTestIdentifier {
pub module_path: ModulePath,
pub test_name: String,
}
impl TestIdentifier for GolangTestIdentifier {}
impl TestIdentifierCore for GolangTestIdentifier {
fn lightly_unique_name(&self) -> String {
self.test_name.clone()
}
}
impl fmt::Display for GolangTestIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} / {}", self.module_path.0, self.test_name)
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub enum GolangCoverageIdentifier {
// Possible future: go version, platform, etc. -- might be better as tags since they'd be pretty universal for the whole commit though?
PackageDependency(ModuleDependency),
InferredFromTestFileChange(PathBuf),
NetworkDependency(ResolvedSocketAddr),
}
impl CoverageIdentifier for GolangCoverageIdentifier {}
impl TryFrom<GolangCoverageIdentifier> for NetworkDependency {
type Error = &'static str;
#[allow(clippy::match_wildcard_for_single_variants)] // really unlikely that new variations will match
fn try_from(value: GolangCoverageIdentifier) -> std::result::Result<Self, Self::Error> {
match value {
GolangCoverageIdentifier::NetworkDependency(socket) => Ok(Self { socket }),
_ => Err("not supported"),
}
}
}
#[derive(Debug, Clone)]
pub struct GolangConcreteTestIdentifier {
test_identifier: GolangTestIdentifier,
_binary_dir: Arc<TempDir>,
binary_path: PathBuf,
}
impl PartialEq for GolangConcreteTestIdentifier {
fn eq(&self, other: &Self) -> bool {
self.test_identifier == other.test_identifier && self.binary_path == other.binary_path
}
}
impl Hash for GolangConcreteTestIdentifier {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.test_identifier.hash(state);
self.binary_path.hash(state);
}
}
impl Eq for GolangConcreteTestIdentifier {}
impl ConcreteTestIdentifier<GolangTestIdentifier> for GolangConcreteTestIdentifier {
fn test_identifier(&self) -> &GolangTestIdentifier {
&self.test_identifier
}
}
pub struct GolangTestDiscovery {
all_test_cases: HashSet<GolangConcreteTestIdentifier>,
}
impl TestDiscovery<GolangConcreteTestIdentifier, GolangTestIdentifier> for GolangTestDiscovery {
fn all_test_cases(&self) -> &HashSet<GolangConcreteTestIdentifier> {
&self.all_test_cases
}
fn map_ti_to_cti(
&self,
test_identifier: GolangTestIdentifier,
) -> Option<GolangConcreteTestIdentifier> {
for cti in &self.all_test_cases {
if cti.test_identifier == test_identifier {
return Some(cti.clone());
}
}
None
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
pub struct ModulePath(pub String); // eg. github.com/shopspring/decimal
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
pub struct BinaryName(pub String); // eg. go-coverage-specimen.out
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct ModuleDependency {
module_path: ModulePath,
version: String,
}
struct ModuleInfo {
module_path: ModulePath,
dependencies: Vec<ModuleDependency>,
}
#[derive(Clone)]
struct GoCoverageData<'a> {
module_and_file: &'a str,
start_marker: &'a str,
end_marker: &'a str,
// _num_statements: &'a str,
hit_count: &'a str,
}
#[derive(Eq, Hash, PartialEq, Debug, Clone)]
struct GoCoverageStatementIdentity {
module_and_file: String,
start_marker: String,
end_marker: String,
}
impl<'a> From<&GoCoverageData<'a>> for (GoCoverageStatementIdentity, i32) {
fn from(data: &GoCoverageData<'a>) -> Self {
let statement_identity = GoCoverageStatementIdentity {
module_and_file: data.module_and_file.to_string(),
start_marker: data.start_marker.to_string(),
end_marker: data.end_marker.to_string(),
};
let hit_count: i32 = data.hit_count.parse().unwrap_or(0); // Converts hit_count to i32, defaults to 0 on parse failure
(statement_identity, hit_count)
}
}
// See comment in guess_tests_from_test_file_changed for why this exists
static TEST_FUNC_DEFINITION_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?xs)
func
\s+
(?<test_name>
Test
[A-Z]
\S+
)
\s* # opt whitespace between name and params
\( # start of function parameters
",
)
.unwrap()
});
// Really hacky regex; probably should use a parser. Supports up to five includes on one line.
static EMBED_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?xm)
^
[\t\v\f\x20]* # optional whitespace, not newlines
//go:embed
(?:
# double-quote w/ path
[\t\v\f\x20]+
"
(?<qpath1>(?:[^"\\]|\\.)*)
"
|
# backtick-quote with path
[\t\v\f\x20]+
`
(?<bpath1>(?:[^`\\]|\\.)*)
`
|
# Unquoted path
[\t\v\f\x20]+
(?<path1>[^\n\r\s]+)
)
(?:
# double-quote w/ path
[\t\v\f\x20]+
"
(?<qpath2>(?:[^"\\]|\\.)*)
"
|
# backtick-quote with path
[\t\v\f\x20]+
`
(?<bpath2>(?:[^`\\]|\\.)*)
`
|
# Unquoted path
[\t\v\f\x20]+
(?<path2>[^\n\r\s]+)
)?
(?:
# double-quote w/ path
[\t\v\f\x20]+
"
(?<qpath3>(?:[^"\\]|\\.)*)
"
|
# backtick-quote with path
[\t\v\f\x20]+
`
(?<bpath3>(?:[^`\\]|\\.)*)
`
|
# Unquoted path
[\t\v\f\x20]+
(?<path3>[^\n\r\s]+)
)?
(?:
# double-quote w/ path
[\t\v\f\x20]+
"
(?<qpath4>(?:[^"\\]|\\.)*)
"
|
# backtick-quote with path
[\t\v\f\x20]+
`
(?<bpath4>(?:[^`\\]|\\.)*)
`
|
# Unquoted path
[\t\v\f\x20]+
(?<path4>[^\n\r\s]+)
)?
(?:
# double-quote w/ path
[\t\v\f\x20]+
"
(?<qpath5>(?:[^"\\]|\\.)*)
"
|
# backtick-quote with path
[\t\v\f\x20]+
`
(?<bpath5>(?:[^`\\]|\\.)*)
`
|
# Unquoted path
[\t\v\f\x20]+
(?<path5>[^\n\r\s]+)
)?
[\t\v\f\x20]* # optional whitespace, not newlines
$
"#,
)
.unwrap()
});
pub struct GolangTestPlatform;
impl GolangTestPlatform {
#[must_use]
pub fn autodetect(project_dir: &Path) -> bool {
if fs::exists(project_dir.join("go.mod"))
.expect("autodetect test project type failed when checking go.mod existence")
{
trace!("Detected go.mod; auto-detect result: Golang test project");
true
} else {
false
}
}
fn get_build_test_command(
module_info: &ModuleInfo,
tmp_dir: &TempDir,
module: &ModulePath,
) -> Command {
// form the coverpkg arg out of all the dependencies
let mut coverpkg = String::with_capacity(1024);
for dep in &module_info.dependencies {
coverpkg.push_str(&dep.module_path.0);
coverpkg.push(',');
}
coverpkg.push_str("./..."); // include this package and all local subpackages
let mut cmd = Command::new("go");
cmd.args([
"test",
"-c",
"-o",
&(String::from(tmp_dir.path().to_string_lossy()) + "/"),
"-json",
"-cover",
"-covermode",
"count",
"-coverpkg",
&coverpkg,
&module.0,
]);
cmd
}
fn get_run_test_command(binary_path: &Path, test_regex: &str, profile_file: &Path) -> Command {
let mut cmd = Command::new(binary_path);
cmd.args([
"-test.run",
test_regex,
"-test.coverprofile",
&profile_file.to_string_lossy(),
]);
cmd
}
// When an external dependency is present in Go, constants and their initialization functions are captured even if
// the library isn't actually touched. For example, in go-coverage-specimen check-8 when an external dependency is
// added, every test will record instrumentation data showing that the external dependency is touched when executed.
// This isn't great because it doesn't allow testtrim to target the tests that actually use that external
// dependency; as long as it has initialization code, it will be tracked as touched during that test.
//
// There is an argument to be made that the behavior is correct: initialization code is executed, and theoretically
// it could have an impact on the test. But for testtrim's purposes we're going to try to be more specific.
//
// testtrim works around this by, for every module that we're running tests, generating a "no-op" test coverage map.
// Basically run a test that doesn't exists (eg. "FooBarTestAbc123987!"), and capture its coverage specifically for
// external dependencies. And then when we run a later test, we'll use that no-op test coverage map as a baseline.
// The external dependency will only be considered a dependency of the test if the coverage map for that external
// dependency varies from the baseline.
//
// `-mode count` causes Go to collect a count for the number of times each branch is touched, rather than a boolean
// 1 or 0 (`-mode set`). The `count` mode is preferred because it causes that external dependency coverage map to
// reliably detect dependency access -- with mode set, if you happened to hit the same codepaths as the
// initialization code during a test, the dependency wouldn't be tracked. (A more aggressive atomic count mode
// exists which makes the counts threadsafe, but that seems unnecessary as the initialization code, I think, can't
// be multithreaded.)
//
// This same problem *probably* exists if you don't have an external dependency too! Const values during package
// initialization would always show as being touched by every test. A future investigation should be done to
// identify the best behavior in this case.
async fn get_baseline_ext(
project_dir: &Path,
test_binary_path: &Path,
tmp_path: &Path,
) -> Result<HashMap<GoCoverageStatementIdentity, i32>> {
let profile_file = tmp_path.join("__baseline__.out");
let mut cmd = Self::get_run_test_command(
test_binary_path,
"^$", // goal is an impossible test name; zero-length string should be impossible?
&profile_file,
);
debug!("running: {cmd:?}");
let output = cmd.current_dir(project_dir).output().await.map_err(|e| {
SubcommandErrors::UnableToStart {
command: format!("{} ...run-noop-test...", test_binary_path.display()).to_string(),
error: e,
}
})?;
if !output.status.success() {
return Err(anyhow!(
"failed to run go test for baseline; exit code: {:?}",
output.status
));
}
let mut retval = HashMap::new();
let reader =
BufReader::new(File::open(profile_file).context("Failed to open profile file")?);
for line in reader.lines() {
let line = line?;
if line.starts_with("mode: ") {
continue;
}
let line = Self::parse_go_coverage_line(&line);
if line.hit_count == "0" {
// No need to keep track of this.
continue;
}
// Currently we don't skip anything from within our own module (eg. using `test_module_name`), and so we'll
// also end up ignoring coverage that is "always present" in our module. It isn't super clear whether
// that's the right thing to do or not.
let extract: (GoCoverageStatementIdentity, i32) = (&line).into();
let (identity, count) = extract;
retval.insert(identity, count);
}
Ok(retval)
}
async fn run_test(
project_dir: &Path,
test_case: &GolangConcreteTestIdentifier,
tmp_path: &Path,
module_info: &ModuleInfo,
package_baseline: &HashMap<ModulePath, HashMap<GoCoverageStatementIdentity, i32>>,
) -> Result<CommitCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>, RunTestError>
{
let mut coverage_data = CommitCoverageData::new();
coverage_data.add_executed_test(test_case.test_identifier.clone());
let coverage_dir = tmp_path.join(&test_case.test_identifier.module_path.0);
// Create coverage_dir but ignore if its error is 17 (file exists)
fs::create_dir_all(&coverage_dir)
.or_else(|e| {
if e.kind() == io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(e)
}
})
.context("Failed to create coverage directory")?;
let profile_file = coverage_dir
.join(&test_case.test_identifier.test_name)
.with_extension("out");
let strace_file = coverage_dir
.join(&test_case.test_identifier.test_name)
.with_extension("strace");
debug!(
"Execute test case {test_case:?} into {}...",
profile_file.display()
);
let (output, trace) = async {
let mut cmd = Self::get_run_test_command(
&test_case.binary_path,
// make sure we're matching the one and only test:
&format!("^{}$", regex::escape(&test_case.test_identifier.test_name)),
&profile_file,
);
cmd.current_dir(project_dir);
SYS_TRACE_COMMAND.trace_command(cmd, &strace_file).await
}
.instrument(info_span!(
"execute-test",
perftrace = "run-test",
parallel = true
))
.await?;
if !output.status.success() {
return Err(RunTestError::TestExecutionFailure(FailedTestResult {
test_identifier: Box::new(test_case.test_identifier.clone()),
failure: TestFailure::NonZeroExitCode {
exit_code: output.status.code(),
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
},
}));
}
let Some(package_baseline) = package_baseline.get(&test_case.test_identifier.module_path)
else {
return Err(RunTestError::Other(anyhow!(
"could not find coverage baseline for module {:?}",
test_case.test_identifier.module_path.0
)));
};
Self::parse_profiling_data(
test_case,
&profile_file,
&mut coverage_data,
module_info,
package_baseline,
)?;
Self::parse_trace_data(project_dir, test_case, &trace, &mut coverage_data)?;
Ok(coverage_data)
}
#[instrument(skip_all, fields(perftrace = "parse-test-data"))]
fn parse_profiling_data(
test_case: &GolangConcreteTestIdentifier,
profile_file: &PathBuf,
coverage_data: &mut CommitCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>,
module_info: &ModuleInfo,
package_baseline: &HashMap<GoCoverageStatementIdentity, i32>,
) -> Result<()> {
let reader =
BufReader::new(File::open(profile_file).context("Failed to open profile file")?);
let mut file_to_module_map = HashSet::new();
for line in reader.lines() {
let line = line?;
if line.starts_with("mode: ") {
continue;
}
let line = Self::parse_go_coverage_line(&line);
if line.hit_count == "0" {
continue;
}
let extract: (GoCoverageStatementIdentity, i32) = (&line).into();
let (identity, current_count) = extract;
if let Some(baseline_count) = package_baseline.get(&identity) {
match baseline_count.cmp(¤t_count) {
Ordering::Equal => {
// Skip this coverage line -- it's the same as our baseline coverage, so nothing new.
continue;
}
Ordering::Less => {
// Good, we've really touched this stmt in this test; the baseline count was lower than the
// current count. Proceed to mark it as a dependency.
}
Ordering::Greater => {
// I think this should never happen; just curious to see if it does
warn!(
"baseline_count {baseline_count} was greater than current count {current_count} for identity {identity:?}"
);
}
}
}
// If we can strip the module name (eg. codeberg.org/testtrim/go-coverage-specimen) from the module + file,
// then it's a file that is relative to our repo and we can record file coverage.
//
// FIXME: we can probably keep track of line.module_and_file and only process it through the rest of this
// function once, as it will likely be repeated many times in the coverage file.
let relative_file = line
.module_and_file
.strip_prefix(&(module_info.module_path.0.clone() + "/"));
if let Some(relative_file) = relative_file {
if relative_file.starts_with('/') {
// strip_prefix has a hack above to ensure it is getting relative files (eg. basic_ops.rs, not
// /basic_ops.rs); this check just raises the visibility of any problem that might occur here
error!("relative_file was incorrectly prefix stripped; {relative_file:?}");
}
trace!(
"test case {:?} touched file {}",
test_case.test_identifier, relative_file
);
coverage_data.add_file_to_test(FileCoverage {
test_identifier: test_case.test_identifier.clone(),
file_name: PathBuf::from(relative_file),
});
} else {
// (otherwise it's a file from a dependency...)
if !file_to_module_map.contains(line.module_and_file) {
// Ideally would use the .insert() retval, but then it would need to clone the module_and_file
// string every time... on the other hand this does two traversals of the hash, so, which is better?
// FIXME: would be fun to micro-benchmark, but probably not important
file_to_module_map.insert(String::from(line.module_and_file));
// First time we've found this module/file; need to resolve it to a dependency:
let mut dependency = None;
for dep in &module_info.dependencies {
if line.module_and_file.starts_with(&dep.module_path.0) {
dependency = Some(dep);
}
}
if let Some(dependency) = dependency {
trace!(
"test case {:?} touched file... {:?} -> {dependency:?}",
test_case.test_identifier, line.module_and_file
);
coverage_data.add_heuristic_coverage_to_test(HeuristicCoverage {
test_identifier: test_case.test_identifier.clone(),
coverage_identifier: GolangCoverageIdentifier::PackageDependency(
ModuleDependency {
module_path: dependency.module_path.clone(),
version: dependency.version.clone(),
},
),
});
} else {
warn!(
"test touched file {:?} but could not identify what dependency this came from",
line.module_and_file
);
}
}
}
}
Ok(())
}
#[instrument(skip_all, fields(perftrace = "parse-test-data"))]
fn parse_trace_data(
project_dir: &Path,
test_case: &GolangConcreteTestIdentifier,
trace: &Trace,
coverage_data: &mut CommitCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>,
) -> Result<()> {
for path in trace.get_open_paths() {
if path.is_relative() || path.starts_with(project_dir) {
debug!(
"found test {} accessed local file {}",
test_case.test_identifier,
path.display()
);
let target_path = normalize_path(
path,
&project_dir.join("fake"), // normalize_path expects relative_to to be a file, not dir; so we add a fake child path
project_dir,
|warning| {
warn!(
"syscall trace accessed path {} but couldn't normalize to repo root: {warning}",
path.display()
);
},
);
debug!("target_path = {target_path:?}");
if let Some(target_path) = target_path {
// It might make sense to filter out files that aren't part of the repo... both here and in
// parse_profiling_data?
coverage_data.add_file_to_test(FileCoverage {
file_name: target_path.clone(),
test_identifier: test_case.test_identifier.clone(),
});
}
}
// FIXME: absolute path case -- check if it's part of the repo/cwd, and if so include it
}
for sockaddr in trace.get_connect_sockets() {
coverage_data.add_heuristic_coverage_to_test(HeuristicCoverage {
test_identifier: test_case.test_identifier.clone(),
coverage_identifier: GolangCoverageIdentifier::NetworkDependency(sockaddr.clone()),
});
}
Ok(())
}
fn parse_go_coverage_line<'a>(line: &'a str) -> GoCoverageData<'a> {
// https://github.com/golang/go/blob/c5d7f2f1cbaca8938a31a022058b1a3300817e33/src/cmd/cover/profile.go#L53-L56
// First line is "mode: foo", where foo is "set", "count", or "atomic".
// Rest of file is in the format
// encoding/base64/base64.go:34.44,37.40 3 1
// where the fields are: name.go:line.column,line.column numberOfStatements count
let parts: Vec<&'a str> = line.split_whitespace().collect();
let file_and_markers: Vec<&'a str> = parts[0].split(':').collect();
let markers: Vec<&'a str> = file_and_markers[1].split(',').collect();
GoCoverageData {
module_and_file: file_and_markers[0],
start_marker: markers[0],
end_marker: markers[1],
// _num_statements: parts[1],
hit_count: parts[2],
}
}
fn parse_module_info() -> Result<ModuleInfo> {
let contents = read_to_string("go.mod")?;
let gomod = parse_gomod(&contents)?;
let mut module_path: Option<String> = None;
let mut dependencies: Vec<ModuleDependency> = vec![];
for item in gomod {
match item.value {
Directive::Module { module_path: mp } => {
module_path = Some(String::from(mp));
}
Directive::Require { specs } => {
for spec in specs {
let (dependency_module_path, version) = spec.value;
dependencies.push(ModuleDependency {
module_path: ModulePath(String::from(dependency_module_path)),
version: String::from(&*version),
});
}
}
// FIXME: Replace, Exclude, Extract -- do these need to be supported?
_ => {}
}
}
if let Some(module_path) = module_path {
Ok(ModuleInfo {
module_path: ModulePath(module_path),
dependencies,
})
} else {
Err(anyhow!("unable to parse `module` identifier from `go.mod`"))
}
}
fn go_mod_test_cases<Commit: ScmCommit, MyScm: Scm<Commit>>(
eval_target_test_cases: &HashSet<GolangTestIdentifier>,
scm: &MyScm,
ancestor_commit: &Commit,
coverage_data: &FullCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>,
test_cases: &mut HashMap<
GolangTestIdentifier,
HashSet<TestReason<GolangCoverageIdentifier>>,
>,
) -> Result<usize> {
// I think there might be plausible cases where Cargo.lock loading from the previous commit would fail, but we
// wouldn't want to error out... for example, if Cargo.lock was added since the ancestor commit?. But I'm not
// confident what those cases would be where we would actually have ancestor coverage data yet be discovering
// Cargo.lock wasn't present? And what behavior we'd want. So for now we'll treat that as an error and wait
// for the situation to appear.
let ancestor_lock = scm.fetch_file_content(ancestor_commit, Path::new("go.mod"))?;
let ancestor_lock = String::from_utf8(ancestor_lock)?;
let ancestor_lock = parse_gomod(&ancestor_lock)?;
let current_lock_data = read_to_string("go.mod")?;
let current_lock = parse_gomod(¤t_lock_data)?;
let mut current_lock_map = HashMap::new();
for item in current_lock {
// FIXME: Replace, Exclude, Extract -- do these need to be supported?
if let Directive::Require { specs } = item.value {
for spec in specs {
let (dependency_module_path, version) = spec.value;
current_lock_map.insert(
ModulePath(String::from(dependency_module_path)),
String::from(&*version),
);
}
}
}
// Cases to consider:
// - Packages with same version in both: Ignore.
// - Packages that have changed from one version to another: search for coverage data based upon old version,
// add tests.
// - Packages that have were present in ancestor_lock and aren't in current_lock: I think also search and add
// those tests?
// - New packages in current_lock that aren't in ancestor_lock aren't relevant -- they wouldn't be part of the
// ancestor's coverage data.
let mut changed_external_dependencies = 0;
for item in ancestor_lock {
// FIXME: Replace, Exclude, Extract -- do these need to be supported?
if let Directive::Require { specs } = item.value {
for spec in specs {
let (old_module_path, old_version) = spec.value;
let old_module_path = ModulePath(String::from(old_module_path));
let old_version = String::from(&*old_version);
let relevant_change = if let Some(current_version) =
current_lock_map.get(&old_module_path)
{
if *current_version == old_version {
false
} else {
trace!(
"go.mod package changed {old_module_path:?}, old: {old_version}, current: {current_version}"
);
true
}
} else {
trace!("go.mod package removed {old_module_path:?}");
true
};
if relevant_change {
info!(
"Change to dependency {old_module_path:?}; will run all tests that touched it"
);
changed_external_dependencies += 1;
let coverage_identifier =
GolangCoverageIdentifier::PackageDependency(ModuleDependency {
module_path: old_module_path,
version: old_version,
});
if let Some(tests) = coverage_data
.coverage_identifier_to_test_map()
.get(&coverage_identifier)
{
for test in tests {
if eval_target_test_cases.contains(test) {
debug!("test {test:?} needs rerun");
test_cases.entry(test.clone()).or_default().insert(
TestReason::CoverageIdentifier(coverage_identifier.clone()),
);
}
}
}
}
}
}
}
Ok(changed_external_dependencies)
}
fn guess_tests_from_test_file_changed(
file: &Path,
all_test_cases: &HashSet<GolangTestIdentifier>,
test_cases: &mut HashMap<
GolangTestIdentifier,
HashSet<TestReason<GolangCoverageIdentifier>>,
>,
source_reason: &TestReason<GolangCoverageIdentifier>,
) -> Result<()> {
if !fs::exists(file)? {
// A file was considered "changed" but doesn't exist -- indicating a deleted file.
return Ok(());
}
// Go doesn't instrument test files (_test.go). (eg.
// https://github.com/golang/go/blob/e0c76d95abfc1621259864adb3d101cf6f1f90fc/src/cmd/go/internal/work/exec.go#L644-L646)
// Ideally we should work upstream to see if we could add this as an optional capability, but that will likely
// be a long path forward. Maybe fun, maybe not.
//
// In the mean time, we're going to do an inaccurate workaround -- read any modified _test.go files and try to
// identify the test cases in them, and mark them as tests that need to be rerun because the _test.go file was
// modified. This is inaccurate for a few reasons:
// - We're not a Go parser, so we're going to do a poor job of parsing the code.
// - We're encoding Go's testing logic outside of Go, which means that it is subject to inaccuracies due to
// change or misunderstanding.
// - Most importantly, it's possible for files in _test.go to refer to public functions defined in each other.
// Coverage-based testing would identify these dependencies, but this hack doesn't -- if you change a_test.go
// and it had a function used by b_test.go, we won't know that the tests in b_test.go need to be rerun.
// However, it's probably "pretty good for most cases"?
let test_file = fs::read_to_string(file)
.context(format!("reading changed test file {}", file.display()))?;
for cap in TEST_FUNC_DEFINITION_REGEX.captures_iter(&test_file) {
let test_name = String::from(&cap["test_name"]);
let mut any_match = false;
for tc in all_test_cases {
if tc.test_name == test_name {
any_match = true;
debug!(
"guessed that modification to {} would require running {tc}",
file.display()
);
test_cases
.entry(tc.clone())
.or_default()
.insert(TestReason::SideEffect(
// Because this happened... probably a FileChanged...
Box::new(source_reason.clone()),
// We did this inference and found this test case should be run.
Box::new(TestReason::CoverageIdentifier(
GolangCoverageIdentifier::InferredFromTestFileChange(
PathBuf::from(file),
),
)),
));
}
}
if !any_match {
warn!(
"inferred that a test named {test_name} exists in file {} but couldn't find it in test cases",
file.display()
);
}
}
Ok(())
}
fn maybe_guess_tests_from_changed_file(
changed_file: &PathBuf,
coverage_data: &FullCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>,
eval_target_test_cases: &HashSet<GolangTestIdentifier>,
test_cases: &mut HashMap<
GolangTestIdentifier,
HashSet<TestReason<GolangCoverageIdentifier>>,
>,
prevent_recursive: &mut HashSet<PathBuf>,
override_reason: Option<&TestReason<GolangCoverageIdentifier>>,
) -> Result<()> {
if !prevent_recursive.insert(changed_file.clone()) {
return Ok(());
}
// Preserve the first file changed as the "reason" for any test cases being included:
let default_reason = TestReason::FileChanged(changed_file.clone());
let reason = override_reason.unwrap_or(&default_reason);
if changed_file
.file_name()
.is_some_and(|name| name.to_string_lossy().ends_with("_test.go"))
{
Self::guess_tests_from_test_file_changed(
changed_file,
eval_target_test_cases,
test_cases,
reason,
)?;
}
// In the event that a _test.go file has a //go:embed in it, the normal process of following referenced files
// (in `compute_changed_file_test_cases`) won't work because we don't have a record of coverage in _test.go
// files, so we won't know what tests to rerun. So we have to duplicate that behavior here with these inferred
// test cases.
if let Some(referencing_files) = coverage_data
.file_referenced_by_files_map()
.get(changed_file)
{
if !referencing_files.is_empty() {
for referencing_file in referencing_files {
Self::maybe_guess_tests_from_changed_file(
referencing_file,
coverage_data,
eval_target_test_cases,
test_cases,
prevent_recursive,
Some(&TestReason::SideEffect(
// Because this occurred (probably a FileChanged)
Box::new(reason.clone()),
// We treated it like this file changed:
Box::new(TestReason::FileChanged(referencing_file.clone())),
)),
)?;
}
}
}
Ok(())
}
#[allow(clippy::manual_map)] // much cleaner as-is than the proposed alternative
fn embed_extract(i: i32, cap: &Captures<'_>) -> Option<PathBuf> {
if let Some(raw) = cap.name(&format!("path{i}")) {
Some(PathBuf::from(raw.as_str()))
} else if let Some(dquote) = cap.name(&format!("qpath{i}")) {
Some(PathBuf::from(dquote.as_str().replace("\\\"", "\"")))
} else if let Some(bquote) = cap.name(&format!("bpath{i}")) {
Some(PathBuf::from(bquote.as_str()))
} else {
None
}
}
fn find_embed_includes(src_file: &PathBuf) -> Result<HashSet<PathBuf>> {
let mut result = HashSet::new();
let Some(parent) = src_file.parent() else {
warn!(
"couldn't resolve path {} to its parent directory",
src_file.display()
);
return Ok(result);
};
let file = File::open(src_file).context(format!(
"error in find_embed_includes opening file {}",
src_file.display()
))?;
let content =
io::read_to_string(BufReader::new(file)).context("find_embed_includes file read")?;
for cap in EMBED_REGEX.captures_iter(&content) {
for i in 1..=5 {
if let Some(path) = Self::embed_extract(i, &cap) {
if path.starts_with("/") || path.starts_with(".") {
// Avoid anything suspicious occurring with path.join; these types of paths aren't supported in
// //go:embed anyway.
continue;
}
let glob_pattern = parent.join(path);
for entry in glob::glob(&glob_pattern.to_string_lossy())? {
let entry = entry?;
if entry.is_dir() {
for dirent in fs::read_dir(entry)? {
let dirent = dirent?;
let entry = dirent.path();
let entry = entry.strip_prefix(parent)?;
result.insert(PathBuf::from(entry));
}
} else {
let entry = entry.strip_prefix(parent)?;
result.insert(PathBuf::from(entry));
}
}
}
}
}
Ok(result)
}
async fn discover_tests_in_module(
project_dir: &Path,
module_info: &ModuleInfo,
module_path: &ModulePath,
) -> Result<HashSet<GolangConcreteTestIdentifier>> {
// FIXME: one potential problem is that we're creating a temp directory, building go programs to it, and then
// executing them. The temp directory space is often configured as a space that can't have executables in it
// (noexec) which could make this fail.
//
// FIXME: this uses an Arc to keep the TempDir from being dropped; it could probably be done instead by making
// the GolangTestDiscovery outlive the ConcreteTestIdentifier and then hoisting the TempDir into the test
// discovery object. That's academically interesting since it's a lifetime problem that would help me learn
// more about lifetime declarations, but, an Arc is just fine too.
//
// FIXME: as a final problem, we're creating one of these temp dirs for every module. I guess that's OK? But
// it seems like maybe we could just create one and use subdirectories.
let tmp_dir = Arc::new(tempfile::Builder::new().prefix("testtrim").tempdir()?);
// First we build all test binaries:
let mut cmd = Self::get_build_test_command(module_info, &tmp_dir, module_path);
debug!("running: {cmd:?}");
let output = cmd
.current_dir(project_dir)
.output()
.instrument(info_span!(
"get test build",
subcommand = true,
subcommand_binary = "go",
subcommand_args = format!("test -c [...] {}", module_path.0), // this isn't technically the args, but the args are huge
))
.await
.map_err(|e| SubcommandErrors::UnableToStart {
command: "go test ...build...".to_string(),
error: e,
})?;
if !output.status.success() {
return Err(SubcommandErrors::SubcommandFailed {
command: String::from("go test -c"),
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
}
.into());
}
trace!("test build success");
// FIXME: since the change to just build one module at a time, we probably don't need to iterate here -- could
// just use one binary.
//
// Now we need to iterate through each of the binaries in tmp_dir and run `... -test.list .` to get the tests
// that they contain:
let mut all_test_cases: HashSet<GolangConcreteTestIdentifier> = HashSet::new();
for dirent in fs::read_dir(tmp_dir.path())? {
let dirent = dirent?;
let mut cmd = Command::new(dirent.path());
let args = ["-test.list", "."];
cmd.args(args);
debug!("running: {cmd:?}");
let output = cmd
.output()
.instrument(info_span!("get test list",
subcommand = true,
subcommand_binary = dirent.path().to_string_lossy().to_string(),
subcommand_args = ?args
))
.await
.map_err(|e| SubcommandErrors::UnableToStart {
command: format!("{:?} -test.list", cmd.as_std().get_program()).to_string(),
error: e,
})?;
if !output.status.success() {
return Err(SubcommandErrors::SubcommandFailed {
command: String::from("'test-binary' -test.list ."),
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
}
.into());
}
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 output");
for line in stdout.lines() {
debug!("Found test case: {line:?}");
all_test_cases.insert(GolangConcreteTestIdentifier {
test_identifier: GolangTestIdentifier {
module_path: module_path.clone(),
test_name: String::from(line),
},
// Hack: make tmp_dir live as long as the test identifiers
_binary_dir: tmp_dir.clone(),
binary_path: dirent.path(),
});
}
}
Ok(all_test_cases)
}
}
impl TestPlatform for GolangTestPlatform {
type TI = GolangTestIdentifier;
type CI = GolangCoverageIdentifier;
type TD = GolangTestDiscovery;
type CTI = GolangConcreteTestIdentifier;
fn platform_identifier() -> &'static str {
"golang"
}
fn platform_tags() -> Vec<Tag> {
vec![Tag {
key: String::from("__testtrim_golang"),
value: String::from("1"),
}]
}
fn project_name(project_dir: &Path) -> Result<String> {
Ok(String::from(
project_dir
.file_name()
.ok_or_else(|| anyhow!("unable to find name of current directory"))?
.to_string_lossy(),
))
}
#[instrument(skip_all, fields(perftrace = "discover-tests"))]
async fn discover_tests(project_dir: &Path) -> Result<GolangTestDiscovery> {
let module_info = Self::parse_module_info()?;
// Discover the modules to work with; this limits it to those with tests:
let mut cmd = Command::new("go");
let args = [
"list",
"-f",
"{{if .TestGoFiles}}{{.ImportPath}}{{end}}",
"./...",
];
cmd.args(args);
cmd.current_dir(project_dir);
debug!("running: {cmd:?}");
let output = cmd
.output()
.instrument(info_span!("go list",
subcommand = true,
subcommand_binary = "go",
subcommand_args = ?args
))
.await
.map_err(|e| SubcommandErrors::UnableToStart {
command: "go list ...discover modules...".to_string(),
error: e,
})?;
if !output.status.success() {
return Err(SubcommandErrors::SubcommandFailed {
command: String::from("go list -f {{if .TestGoFiles}}{{.ImportPath}}{{end}} ./..."),
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
}
.into());
}
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 output");
let mut all_test_cases: HashSet<GolangConcreteTestIdentifier> = HashSet::new();
let all_test_cases = async {
// FIXME: we might be able to do this in parallel to reduce build times... or maybe there's some way we can make
// the go cmdline build multiple packages like this?
for line in stdout.lines() {
let module_path = ModulePath(String::from(line));
all_test_cases.extend(
Self::discover_tests_in_module(project_dir, &module_info, &module_path).await?,
);
}
Ok::<_, anyhow::Error>(all_test_cases)
}
.instrument(info_span!(
"go test",
ui_stage = Into::<u64>::into(UiStage::Compiling)
))
.await?;
// FIXME: compiling and listing tests is integrated together into `discover_tests_in_module` right now, making
// instrumenting these (and therefore displaying a separate UI) require a prereq of splitting them apart. As a
// temporary measure just emit the ListingTests event for the UI at a zero-length span.
info_span!(
"fake list tests",
ui_stage = Into::<u64>::into(UiStage::ListingTests)
)
.in_scope(|| {});
Ok(GolangTestDiscovery { all_test_cases })
}
#[instrument(skip_all, fields(perftrace = "platform-specific-test-cases"))]
fn platform_specific_relevant_test_cases<Commit: ScmCommit, MyScm: Scm<Commit>>(
eval_target_test_cases: &HashSet<GolangTestIdentifier>,
eval_target_changed_files: &HashSet<PathBuf>,
scm: &MyScm,
ancestor_commit: &Commit,
coverage_data: &FullCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>,
) -> Result<PlatformSpecificRelevantTestCaseData<GolangTestIdentifier, GolangCoverageIdentifier>>
{
let mut test_cases: HashMap<
GolangTestIdentifier,
HashSet<TestReason<GolangCoverageIdentifier>>,
> = HashMap::new();
let mut external_dependencies_changed = None;
if eval_target_changed_files.contains(Path::new("go.mod")) {
external_dependencies_changed = Some(Self::go_mod_test_cases(
eval_target_test_cases,
scm,
ancestor_commit,
coverage_data,
&mut test_cases,
)?);
}
let mut prevent_recursive: HashSet<PathBuf> = HashSet::new();
for file in eval_target_changed_files {
Self::maybe_guess_tests_from_changed_file(
file,
coverage_data,
eval_target_test_cases,
&mut test_cases,
&mut prevent_recursive,
None,
)?;
}
Ok(PlatformSpecificRelevantTestCaseData {
additional_test_cases: test_cases,
external_dependencies_changed,
})
}
async fn run_tests<'a, I>(
_test_discovery: &GolangTestDiscovery,
project_dir: &Path,
test_cases: I,
jobs: u16,
) -> Result<CommitCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>, RunTestsErrors>
where
I: IntoIterator<Item = &'a GolangConcreteTestIdentifier>,
GolangConcreteTestIdentifier: 'a,
{
let tmp_dir = tempfile::Builder::new().prefix("testtrim").tempdir()?;
let module_info =
Self::parse_module_info().map_err(|e| RunTestsErrors::PlatformError(e.to_string()))?;
// Will need to collect get_baseline_ext for each module being tested... (At least, I think so? Dependency
// access and initialization seems like something that wouldn't be constant across the entire project?)
let mut vec_test_cases = vec![];
let mut package_baseline = HashMap::new();
for test_case in test_cases {
if !package_baseline.contains_key(&test_case.test_identifier.module_path) {
package_baseline.insert(
test_case.test_identifier.module_path.clone(),
Self::get_baseline_ext(project_dir, &test_case.binary_path, tmp_dir.path())
.await
.map_err(|e| RunTestsErrors::PlatformError(e.to_string()))?,
);
}
vec_test_cases.push(test_case);
}
let package_baseline_ref = &package_baseline;
let mut futures = vec![];
for test_case in vec_test_cases {
let tc = test_case.clone();
let tmp_path = PathBuf::from(tmp_dir.path());
let module_info_ref = &module_info;
futures.push(async move {
GolangTestPlatform::run_test(
project_dir,
&tc,
&tmp_path,
module_info_ref,
package_baseline_ref,
)
.instrument(info_span!("go test",
ui_stage = Into::<u64>::into(UiStage::RunSingleTest),
test_case = %tc.test_identifier(),
))
.await
});
}
let concurrency = if jobs == 0 {
num_cpus::get()
} else {
jobs.into()
};
let results = spawn_limited_concurrency(concurrency, futures).await;
let mut failed_test_results = vec![];
let mut coverage_data = CommitCoverageData::new();
for result in results {
match result {
Ok(res) => coverage_data.merge_in(res),
Err(RunTestError::TestExecutionFailure(failed_test_result)) => {
failed_test_results.push(failed_test_result);
}
Err(e) => return Err(e.into()),
}
}
if failed_test_results.is_empty() {
Ok(coverage_data)
} else {
Err(RunTestsErrors::TestExecutionFailures(failed_test_results))
}
}
fn analyze_changed_files(
project_dir: &Path,
changed_files: &HashSet<PathBuf>,
coverage_data: &mut CommitCoverageData<GolangTestIdentifier, GolangCoverageIdentifier>,
) -> Result<()> {
for file in changed_files {
if file.extension().is_some_and(|ext| ext == "go") {
let mut found_references = false;
if !fs::exists(file)? {
// A file was considered "changed" but doesn't exist -- indicating a deleted file.
coverage_data.mark_file_makes_no_references(file.clone());
continue;
}
for target_path in Self::find_embed_includes(file)? {
debug!(
"found that {} references {}",
file.display(),
target_path.display()
);
// FIXME: It's not clear whether warnings are the right behavior for any of these problems. Some of
// them might be better elevated to errors?
let target_path = normalize_path(&target_path, file, project_dir, |warning| {
warn!(
"file {} had a //go:embed, but reference could not be followed: {warning}",
file.display(),
);
});
if let Some(target_path) = target_path {
coverage_data.add_file_reference(FileReference {
referencing_file: file.clone(),
target_file: target_path,
});
found_references = true;
}
}
if !found_references {
coverage_data.mark_file_makes_no_references(file.clone());
}
} else {
// This probably isn't necessary since it would've never been marked as making references
coverage_data.mark_file_makes_no_references(file.clone());
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::platform::golang::GolangTestPlatform;
use super::TEST_FUNC_DEFINITION_REGEX;
#[test]
fn test_parse_go_coverage_line() {
let line = "encoding/base64/base64.go:34.44,37.40 3 1";
let coverage_data = GolangTestPlatform::parse_go_coverage_line(line);
assert_eq!(coverage_data.module_and_file, "encoding/base64/base64.go");
assert_eq!(coverage_data.start_marker, "34.44");
assert_eq!(coverage_data.end_marker, "37.40");
// assert_eq!(coverage_data._num_statements, "3");
assert_eq!(coverage_data.hit_count, "1");
let line = "github.com/shopspring/decimal/rounding.go:112.14,114.4 1 317";
let coverage_data = GolangTestPlatform::parse_go_coverage_line(line);
assert_eq!(
coverage_data.module_and_file,
"github.com/shopspring/decimal/rounding.go"
);
assert_eq!(coverage_data.start_marker, "112.14");
assert_eq!(coverage_data.end_marker, "114.4");
// assert_eq!(coverage_data._num_statements, "1");
assert_eq!(coverage_data.hit_count, "317");
}
#[test]
fn test_test_func_definition() {
let code = r#"
func TestAdd(t *testing.T) {
got := Add(2, 3)
if got != 5 {
t.Errorf("Add(2, 3) = %d; want 5", got)
}
got = Add(-1, 1)
if got != 0 {
t.Errorf("Add(-1, 1) = %d; want 0", got)
}
}
func TestAddDecimal(t *testing.T) {
got := AddDecimal(decimal.NewFromInt(2), decimal.NewFromInt(3))
if !got.Equal(decimal.NewFromInt(5)) {
t.Errorf("AddDecimal(2, 3) = %d; want 5", got)
}
got = AddDecimal(decimal.NewFromInt(-1), decimal.NewFromInt(1))
if !got.Equal(decimal.NewFromInt(0)) {
t.Errorf("AddDecimal(-1, 1) = %d; want 0", got)
}
}
"#;
let caps = TEST_FUNC_DEFINITION_REGEX
.captures_iter(code)
.collect::<Vec<_>>();
assert_eq!(caps.len(), 2, "expected two Test... functions to be found");
assert_eq!(&caps[0]["test_name"], "TestAdd");
}
#[test]
fn find_compile_time_includes() {
let res = GolangTestPlatform::find_embed_includes(&PathBuf::from(
"tests/go_parse_examples/embed.go",
));
assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(res.len(), 6, "correct # of files read; res={res:?}");
assert!(res.contains(&PathBuf::from("file1.txt")));
assert!(res.contains(&PathBuf::from("file2.txt"))); // multiple includes on one line
assert!(res.contains(&PathBuf::from("file3.txt")));
assert!(res.contains(&PathBuf::from("dir1/file4.txt"))); // directory include
assert!(res.contains(&PathBuf::from("dir1/file5.txt")));
assert!(res.contains(&PathBuf::from("dir2/file6.txt"))); // glob include
}
}