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
|
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! Functions to extract finalized flag information from
//! /prebuilts/sdk/#/finalized-flags.txt.
//! These functions are very specific to that file setup as well as the format
//! of the files (just a list of the fully-qualified flag names).
//! There are also some helper functions for local building using cargo. These
//! functions are only invoked via cargo for quick local testing and will not
//! be used during actual soong building. They are marked as such.
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::{self, BufRead};
use std::str::FromStr;
/// Mirrors the Java API in android.os.Build.
const SDK_INT_MULTIPLIER: u32 = 100_000;
/// SDK_INT_FULL was introduced in Baklava, so checking SDK_INT_FULL on a lower
/// version would throw an exception. Therefore, we shouldn't allow the creation
/// of a lower version.
const MIN_SDK_INT_FULL: u32 = SDK_INT_MULTIPLIER * 36;
/// Just the fully qualified flag name (package_name.flag_name).
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct FinalizedFlag {
/// Name of the flag.
pub flag_name: String,
/// Name of the package.
pub package_name: String,
}
/// API level check for the flag.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct ApiLevel(pub u32);
impl ApiLevel {
/// Creates an api level check against Build.VERSION.SDK_INT.
pub fn from_sdk_int(level: u32) -> Self {
if level > SDK_INT_MULTIPLIER {
panic!("SDK_INT value too large.");
}
ApiLevel(level)
}
/// Creates an api level check against Build.VERSION.SDK_INT_FULL.
/// Since SDK_INT_FULL was introduced in Baklava, only works for levels above
/// 3_600_000 (panics otherwise).
pub fn from_sdk_int_full(level: u32) -> Self {
if level < MIN_SDK_INT_FULL {
panic!("Cannot use SDK_INT_FULL below Baklava (36).");
}
ApiLevel(level)
}
/// Returns the string condition to check if the flag is finalized on device
/// in Java.
pub fn conditional(&self) -> String {
if self.0 < SDK_INT_MULTIPLIER {
format!("Build.VERSION.SDK_INT >= {}", self.0)
} else if self.0 < MIN_SDK_INT_FULL {
panic!("Invalid SDK level ({}) - greater than the multiplier but less than the supported level.", self.0);
} else {
format!("Build.VERSION.SDK_INT >= 36 && Build.VERSION.SDK_INT_FULL >= {}", self.0)
}
}
}
impl FromStr for ApiLevel {
type Err = anyhow::Error;
/// Converts a string to the appropriate ApiLevel.
fn from_str(s: &str) -> Result<Self, Self::Err> {
let float_value = s.parse::<f64>()?;
if float_value.fract() == 0.0 {
return Ok(ApiLevel::from_sdk_int(float_value as u32));
}
if cfg!(feature = "support_minor_sdk") {
match parse_full_version(s.to_string()) {
Ok(full_sdk_int) => Ok(ApiLevel::from_sdk_int_full(full_sdk_int)),
Err(e) => Err(e),
}
} else {
Err(anyhow!("Numeric string is float, can't parse to int."))
}
}
}
/// Contains all flags finalized for a given API level.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct FinalizedFlagMap(HashMap<ApiLevel, HashSet<FinalizedFlag>>);
impl FinalizedFlagMap {
/// Creates a new, empty instance.
pub fn new() -> Self {
Self(HashMap::new())
}
/// Convenience method for is_empty on the underlying map.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns the API level in which the flag was finalized .
pub fn get_finalized_level(&self, flag: &FinalizedFlag) -> Option<ApiLevel> {
for (api_level, flags_for_level) in &self.0 {
if flags_for_level.contains(flag) {
return Some(api_level.clone());
}
}
None
}
/// Insert the flag into the map for the given level if the flag is not
/// present in the map already - for *any* level (not just the one given).
pub fn insert_if_new(&mut self, level: ApiLevel, flag: FinalizedFlag) {
if self.contains(&flag) {
return;
}
self.0.entry(level).or_default().insert(flag);
}
fn contains(&self, flag: &FinalizedFlag) -> bool {
self.0.values().any(|flags_set| flags_set.contains(flag))
}
}
fn parse_full_version(version: String) -> Result<u32> {
let (major, minor) = if let Some(decimal_index) = version.find('.') {
(version[..decimal_index].parse::<u32>()?, version[decimal_index + 1..].parse::<u32>()?)
} else {
(version.parse::<u32>()?, 0)
};
if major >= 21474 {
return Err(anyhow!("Major version too large, must be less than 21474."));
}
if minor >= SDK_INT_MULTIPLIER {
return Err(anyhow!("Minor version too large, must be less than {}.", SDK_INT_MULTIPLIER));
}
Ok(major * SDK_INT_MULTIPLIER + minor)
}
/// For each file, extracts the qualified flag names into a FinalizedFlag, then
/// enters them in a map at the API level corresponding to their directory.
/// Ex: /prebuilts/sdk/35/finalized-flags.txt -> {36, [flag1, flag2]}.
pub fn read_files_to_map_using_path(flag_files: Vec<String>) -> Result<FinalizedFlagMap> {
let mut data_map = FinalizedFlagMap::new();
for flag_file in flag_files {
// Split /path/sdk/<int.int>/finalized-flags.txt -> ['/path/sdk', 'int.int', 'finalized-flags.txt'].
let flag_file_split: Vec<String> =
flag_file.clone().rsplitn(3, '/').map(|s| s.to_string()).collect();
if &flag_file_split[0] != "finalized-flags.txt"
&& &flag_file_split[0] != "extended_flags_list.txt"
{
return Err(anyhow!(
"Provided incorrect file, must be finalized-flags.txt or extended_flags_list.txt"
));
}
let api_level_string = &flag_file_split[1];
// In the future, we should error if provided a non-numeric directory.
let Ok(api_level) = ApiLevel::from_str(api_level_string) else {
continue;
};
let file = fs::File::open(&flag_file)?;
io::BufReader::new(file).lines().for_each(|flag| {
let flag =
flag.unwrap_or_else(|_| panic!("Failed to read line from file {}", flag_file));
let finalized_flag = build_finalized_flag(&flag)
.unwrap_or_else(|_| panic!("cannot build finalized flag {}", flag));
data_map.insert_if_new(api_level.clone(), finalized_flag);
});
}
Ok(data_map)
}
fn build_finalized_flag(qualified_flag_name: &String) -> Result<FinalizedFlag> {
// Split the qualified flag name into package and flag name:
// com.my.package.name.my_flag_name -> ('com.my.package.name', 'my_flag_name')
let (package_name, flag_name) = qualified_flag_name
.rsplit_once('.')
.ok_or(anyhow!("Invalid qualified flag name format: '{}'", qualified_flag_name))?;
Ok(FinalizedFlag { flag_name: flag_name.to_string(), package_name: package_name.to_string() })
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
const FLAG_FILE_NAME: &str = "finalized-flags.txt";
// Creates some flags for testing.
fn create_test_flags() -> Vec<FinalizedFlag> {
vec![
FinalizedFlag { flag_name: "name1".to_string(), package_name: "package1".to_string() },
FinalizedFlag { flag_name: "name2".to_string(), package_name: "package2".to_string() },
FinalizedFlag { flag_name: "name3".to_string(), package_name: "package3".to_string() },
]
}
// Writes the fully qualified flag names in the given file.
fn add_flags_to_file(flag_file: &mut File, flags: &[FinalizedFlag]) {
for flag in flags {
let _unused = writeln!(flag_file, "{}.{}", flag.package_name, flag.flag_name);
}
}
#[test]
fn test_read_flags_one_file() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt.
let temp_dir = tempdir().unwrap();
let mut file_path = temp_dir.path().to_path_buf();
file_path.push("35");
fs::create_dir_all(&file_path).unwrap();
file_path.push(FLAG_FILE_NAME);
let mut file = File::create(&file_path).unwrap();
// Write all flags to the file.
add_flags_to_file(&mut file, &[flags[0].clone(), flags[1].clone()]);
let flag_file_path = file_path.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![flag_file_path]).unwrap();
assert_eq!(map.0.len(), 1);
assert!(map.0.get(&ApiLevel::from_sdk_int(35)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel::from_sdk_int(35)).unwrap().contains(&flags[1]));
}
#[test]
fn test_read_flags_two_files() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt and for 36.
let temp_dir = tempdir().unwrap();
let mut file_path1 = temp_dir.path().to_path_buf();
file_path1.push("35");
fs::create_dir_all(&file_path1).unwrap();
file_path1.push(FLAG_FILE_NAME);
let mut file1 = File::create(&file_path1).unwrap();
let mut file_path2 = temp_dir.path().to_path_buf();
file_path2.push("36");
fs::create_dir_all(&file_path2).unwrap();
file_path2.push(FLAG_FILE_NAME);
let mut file2 = File::create(&file_path2).unwrap();
// Write all flags to the files.
add_flags_to_file(&mut file1, &[flags[0].clone()]);
add_flags_to_file(&mut file2, &[flags[0].clone(), flags[1].clone(), flags[2].clone()]);
let flag_file_path1 = file_path1.to_string_lossy().to_string();
let flag_file_path2 = file_path2.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![flag_file_path1, flag_file_path2]).unwrap();
// Assert there are two API levels, 35 and 36.
assert_eq!(map.0.len(), 2);
assert!(map.0.get(&ApiLevel::from_sdk_int(35)).unwrap().contains(&flags[0]));
// 36 should not have the first flag in the set, as it was finalized in
// an earlier API level.
assert!(map.0.get(&ApiLevel::from_sdk_int(36)).unwrap().contains(&flags[1]));
assert!(map.0.get(&ApiLevel::from_sdk_int(36)).unwrap().contains(&flags[2]));
}
#[test]
fn test_read_flags_full_numbers() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt and for 36.
let temp_dir = tempdir().unwrap();
let mut file_path1 = temp_dir.path().to_path_buf();
file_path1.push("35.0");
fs::create_dir_all(&file_path1).unwrap();
file_path1.push(FLAG_FILE_NAME);
let mut file1 = File::create(&file_path1).unwrap();
let mut file_path2 = temp_dir.path().to_path_buf();
file_path2.push("36.0");
fs::create_dir_all(&file_path2).unwrap();
file_path2.push(FLAG_FILE_NAME);
let mut file2 = File::create(&file_path2).unwrap();
// Write all flags to the files.
add_flags_to_file(&mut file1, &[flags[0].clone()]);
add_flags_to_file(&mut file2, &[flags[0].clone(), flags[1].clone(), flags[2].clone()]);
let flag_file_path1 = file_path1.to_string_lossy().to_string();
let flag_file_path2 = file_path2.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![flag_file_path1, flag_file_path2]).unwrap();
assert_eq!(map.0.len(), 2);
assert!(map.0.get(&ApiLevel::from_sdk_int(35)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel::from_sdk_int(36)).unwrap().contains(&flags[1]));
assert!(map.0.get(&ApiLevel::from_sdk_int(36)).unwrap().contains(&flags[2]));
}
#[cfg(not(feature = "support_minor_sdk"))]
#[test]
fn test_read_flags_fractions_round_up() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt and for 36.
let temp_dir = tempdir().unwrap();
let mut file_path1 = temp_dir.path().to_path_buf();
file_path1.push("36.1");
fs::create_dir_all(&file_path1).unwrap();
file_path1.push(FLAG_FILE_NAME);
let mut file1 = File::create(&file_path1).unwrap();
let mut file_path2 = temp_dir.path().to_path_buf();
file_path2.push("37.0");
fs::create_dir_all(&file_path2).unwrap();
file_path2.push(FLAG_FILE_NAME);
let mut file2 = File::create(&file_path2).unwrap();
// Write all flags to the files.
add_flags_to_file(&mut file1, &[flags[0].clone()]);
add_flags_to_file(&mut file2, &[flags[0].clone(), flags[1].clone(), flags[2].clone()]);
let flag_file_path1 = file_path1.to_string_lossy().to_string();
let flag_file_path2 = file_path2.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![flag_file_path1, flag_file_path2]).unwrap();
// No flags were added in 35. All 35.1 flags were rolled up to 36.
assert_eq!(map.0.len(), 1);
assert!(!map.0.contains_key(&ApiLevel::from_sdk_int(36)));
assert!(map.0.get(&ApiLevel::from_sdk_int(37)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel::from_sdk_int(37)).unwrap().contains(&flags[1]));
assert!(map.0.get(&ApiLevel::from_sdk_int(37)).unwrap().contains(&flags[2]));
}
#[cfg(feature = "support_minor_sdk")]
#[test]
fn test_read_flags_fractions_creates_full_sdk() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt and for 36.
let temp_dir = tempdir().unwrap();
let mut file_path1 = temp_dir.path().to_path_buf();
file_path1.push("36.1");
fs::create_dir_all(&file_path1).unwrap();
file_path1.push(FLAG_FILE_NAME);
let mut file1 = File::create(&file_path1).unwrap();
let mut file_path2 = temp_dir.path().to_path_buf();
file_path2.push("37.0");
fs::create_dir_all(&file_path2).unwrap();
file_path2.push(FLAG_FILE_NAME);
let mut file2 = File::create(&file_path2).unwrap();
// Write all flags to the files.
add_flags_to_file(&mut file1, &[flags[0].clone()]);
add_flags_to_file(&mut file2, &[flags[0].clone(), flags[1].clone(), flags[2].clone()]);
let flag_file_path1 = file_path1.to_string_lossy().to_string();
let flag_file_path2 = file_path2.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![flag_file_path1, flag_file_path2]).unwrap();
// Support 35.1 and 36.0.
assert_eq!(map.0.len(), 2);
assert!(!map.0.contains_key(&ApiLevel::from_sdk_int(36)));
assert!(map.0.get(&ApiLevel::from_sdk_int_full(3_600_001)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel::from_sdk_int(37)).unwrap().contains(&flags[1]));
assert!(map.0.get(&ApiLevel::from_sdk_int(37)).unwrap().contains(&flags[2]));
}
#[test]
fn test_read_flags_non_numeric() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt.
let temp_dir = tempdir().unwrap();
let mut file_path = temp_dir.path().to_path_buf();
file_path.push("35");
fs::create_dir_all(&file_path).unwrap();
file_path.push(FLAG_FILE_NAME);
let mut flag_file = File::create(&file_path).unwrap();
let mut invalid_path = temp_dir.path().to_path_buf();
invalid_path.push("sdk-annotations");
fs::create_dir_all(&invalid_path).unwrap();
invalid_path.push(FLAG_FILE_NAME);
File::create(&invalid_path).unwrap();
// Write all flags to the file.
add_flags_to_file(&mut flag_file, &[flags[0].clone(), flags[1].clone()]);
let flag_file_path = file_path.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![
flag_file_path,
invalid_path.to_string_lossy().to_string(),
])
.unwrap();
// No set should be created for sdk-annotations.
assert_eq!(map.0.len(), 1);
assert!(map.0.get(&ApiLevel::from_sdk_int(35)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel::from_sdk_int(35)).unwrap().contains(&flags[1]));
}
#[test]
fn test_read_flags_wrong_file_err() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt.
let temp_dir = tempdir().unwrap();
let mut file_path = temp_dir.path().to_path_buf();
file_path.push("35");
fs::create_dir_all(&file_path).unwrap();
file_path.push(FLAG_FILE_NAME);
let mut flag_file = File::create(&file_path).unwrap();
let mut pre_flag_path = temp_dir.path().to_path_buf();
pre_flag_path.push("18");
fs::create_dir_all(&pre_flag_path).unwrap();
pre_flag_path.push("some_random_file.txt");
File::create(&pre_flag_path).unwrap();
// Write all flags to the file.
add_flags_to_file(&mut flag_file, &[flags[0].clone(), flags[1].clone()]);
let flag_file_path = file_path.to_string_lossy().to_string();
// Convert to map.
let map = read_files_to_map_using_path(vec![
flag_file_path,
pre_flag_path.to_string_lossy().to_string(),
]);
assert!(map.is_err());
}
#[test]
fn test_flags_map_insert_if_new() {
let flags = create_test_flags();
let mut map = FinalizedFlagMap::new();
let l35 = ApiLevel::from_sdk_int(35);
let l36 = ApiLevel::from_sdk_int(36);
map.insert_if_new(l35.clone(), flags[0].clone());
map.insert_if_new(l35.clone(), flags[1].clone());
map.insert_if_new(l35.clone(), flags[2].clone());
map.insert_if_new(l36.clone(), flags[0].clone());
assert!(map.0.get(&l35.clone()).unwrap().contains(&flags[0]));
assert!(map.0.get(&l35.clone()).unwrap().contains(&flags[1]));
assert!(map.0.get(&l35.clone()).unwrap().contains(&flags[2]));
assert!(!map.0.contains_key(&l36.clone()));
}
#[test]
fn test_flags_map_get_level() {
let flags = create_test_flags();
let mut map = FinalizedFlagMap::new();
let l35 = ApiLevel::from_sdk_int(35);
let l36 = ApiLevel::from_sdk_int(36);
map.insert_if_new(l35, flags[0].clone());
map.insert_if_new(l36, flags[1].clone());
assert_eq!(
map.get_finalized_level(&flags[0]).unwrap(),
//ApiLevel("Build.VERSION.SDK_INT >= 35".to_string())
ApiLevel(35)
);
assert_eq!(
map.get_finalized_level(&flags[1]).unwrap(),
//ApiLevel("Build.VERSION.SDK_INT >= 36".to_string())
ApiLevel(36)
);
}
#[test]
fn test_read_flag_from_extended_file() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/extended_flags_list.txt
let temp_dir = tempdir().unwrap();
let mut file_path = temp_dir.path().to_path_buf();
file_path.push("35");
fs::create_dir_all(&file_path).unwrap();
file_path.push("extended_flags_list.txt");
let mut file = File::create(&file_path).unwrap();
// Write all flags to the file.
add_flags_to_file(&mut file, &[flags[0].clone(), flags[1].clone()]);
let map =
read_files_to_map_using_path(vec![file_path.to_string_lossy().to_string()]).unwrap();
assert_eq!(map.0.len(), 1);
assert!(map.0.get(&ApiLevel(35)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel(35)).unwrap().contains(&flags[1]));
}
#[test]
fn test_read_flags_sdk_file_and_extended_file() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/finalized-flags.txt
let temp_dir = tempdir().unwrap();
let mut file_path1 = temp_dir.path().to_path_buf();
file_path1.push("35");
fs::create_dir_all(&file_path1).unwrap();
file_path1.push(FLAG_FILE_NAME);
let mut file1 = File::create(&file_path1).unwrap();
// Create the file <temp_dir>/36/finalized-flags.txt
let temp_dir = tempdir().unwrap();
let mut file_path2 = temp_dir.path().to_path_buf();
file_path2.push("36");
fs::create_dir_all(&file_path2).unwrap();
file_path2.push(FLAG_FILE_NAME);
let mut file2 = File::create(&file_path2).unwrap();
// Create the file <temp_dir>/36/extended_flags_list.txt
let mut file_path3 = temp_dir.path().to_path_buf();
file_path3.push("36");
fs::create_dir_all(&file_path3).unwrap();
file_path3.push("extended_flags_list.txt");
let mut file3 = File::create(&file_path3).unwrap();
// Write all flags to the files.
add_flags_to_file(&mut file1, &[flags[0].clone()]);
add_flags_to_file(&mut file2, &[flags[0].clone(), flags[1].clone()]);
add_flags_to_file(&mut file3, &[flags[0].clone(), flags[1].clone(), flags[2].clone()]);
let flag_file_path1 = file_path1.to_string_lossy().to_string();
let flag_file_path2 = file_path2.to_string_lossy().to_string();
let flag_file_path3 = file_path3.to_string_lossy().to_string();
// Convert to map.
let map =
read_files_to_map_using_path(vec![flag_file_path1, flag_file_path2, flag_file_path3])
.unwrap();
// Assert there are two API levels, 35 and 36.
assert_eq!(map.0.len(), 2);
assert!(map.0.get(&ApiLevel(35)).unwrap().contains(&flags[0]));
// 36 should not have the first flag in the set, as it was finalized in
// an earlier API level.
assert!(!map.0.get(&ApiLevel(36)).unwrap().contains(&flags[0]));
assert!(map.0.get(&ApiLevel(36)).unwrap().contains(&flags[1]));
assert!(map.0.get(&ApiLevel(36)).unwrap().contains(&flags[2]));
}
#[test]
fn test_read_flag_from_wrong_extended_file_err() {
let flags = create_test_flags();
// Create the file <temp_dir>/35/bar.txt
let temp_dir = tempdir().unwrap();
let mut file_path = temp_dir.path().to_path_buf();
file_path.push("35");
fs::create_dir_all(&file_path).unwrap();
file_path.push("bar.txt");
let mut file = File::create(&file_path).unwrap();
// Write all flags to the file.
add_flags_to_file(&mut file, &[flags[0].clone(), flags[1].clone()]);
let err = read_files_to_map_using_path(vec![file_path.to_string_lossy().to_string()])
.unwrap_err();
assert_eq!(
format!("{:?}", err),
"Provided incorrect file, must be finalized-flags.txt or extended_flags_list.txt"
);
}
#[test]
fn test_parse_full_version_correct_input_major_dot_minor() {
let version = parse_full_version("12.34".to_string());
assert!(version.is_ok());
assert_eq!(version.unwrap(), 1_200_034);
}
#[test]
fn test_parse_full_version_correct_input_omit_dot_minor() {
let version = parse_full_version("1234".to_string());
assert!(version.is_ok());
assert_eq!(version.unwrap(), 123_400_000);
}
#[test]
fn test_parse_full_version_incorrect_input_empty_string() {
let version = parse_full_version("".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_no_numbers_in_string() {
let version = parse_full_version("hello".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_unexpected_patch_version() {
let version = parse_full_version("1.2.3".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_leading_dot_missing_major_version() {
let version = parse_full_version(".1234".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_trailing_dot_missing_minor_version() {
let version = parse_full_version("1234.".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_negative_major_version() {
let version = parse_full_version("-12.34".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_negative_minor_version() {
let version = parse_full_version("12.-34".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_major_version_too_large() {
let version = parse_full_version("40000.1".to_string());
assert!(version.is_err());
}
#[test]
fn test_parse_full_version_incorrect_input_minor_version_too_large() {
let version = parse_full_version("3.99999999".to_string());
assert!(version.is_err());
}
}
|