-
Notifications
You must be signed in to change notification settings - Fork 42
/
parse.rs
1081 lines (944 loc) · 36.7 KB
/
parse.rs
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
use std::{borrow::Cow, ops::Not, str::FromStr};
use nom::{
branch::alt,
bytes::complete::{tag, take_till1, take_until, take_while, take_while1},
character::complete::{char, multispace0, one_of, space0},
combinator::{opt, recognize},
error::{context, ContextError, ParseError},
multi::{separated_list0, separated_list1},
sequence::{delimited, preceded, separated_pair, terminated},
Finish, IResult,
};
use rattler_digest::{parse_digest_from_hex, Md5, Sha256};
use smallvec::SmallVec;
use thiserror::Error;
use typed_path::Utf8TypedPath;
use url::Url;
use super::{
matcher::{StringMatcher, StringMatcherParseError},
MatchSpec,
};
use crate::package::ArchiveIdentifier;
use crate::{
build_spec::{BuildNumberSpec, ParseBuildNumberSpecError},
utils::{path::is_path, url::parse_scheme},
version_spec::{
is_start_of_version_constraint,
version_tree::{recognize_constraint, recognize_version},
ParseVersionSpecError,
},
Channel, ChannelConfig, InvalidPackageNameError, NamelessMatchSpec, PackageName,
ParseChannelError, ParseStrictness,
ParseStrictness::{Lenient, Strict},
ParseVersionError, VersionSpec,
};
/// The type of parse error that occurred when parsing match spec.
#[derive(Debug, Clone, Error, PartialEq)]
pub enum ParseMatchSpecError {
/// The path or url of the package was invalid
#[error("invalid package path or url")]
InvalidPackagePathOrUrl,
/// Invalid package spec url
#[error("invalid package spec url")]
InvalidPackageUrl(#[from] url::ParseError),
/// Invalid version in path or url
#[error(transparent)]
InvalidPackagePathOrUrlVersion(#[from] ParseVersionError),
/// Invalid bracket in match spec
#[error("invalid bracket")]
InvalidBracket,
/// Invalid number of colons in match spec
#[error("invalid number of colons")]
InvalidNumberOfColons,
/// Invalid channel provided in match spec
#[error("invalid channel")]
ParseChannelError(#[from] ParseChannelError),
/// Invalid key in match spec
#[error("invalid bracket key: {0}")]
InvalidBracketKey(String),
/// Missing package name in match spec
#[error("missing package name")]
MissingPackageName,
/// Multiple bracket sections in match spec
#[error("multiple bracket sections not allowed")]
MultipleBracketSectionsNotAllowed,
/// Invalid version and build
#[error("Unable to parse version spec: {0}")]
InvalidVersionAndBuild(String),
/// Invalid build string
#[error("The build string '{0}' is not valid it can only contain alphanumeric characters and underscores")]
InvalidBuildString(String),
/// Invalid version spec
#[error(transparent)]
InvalidVersionSpec(#[from] ParseVersionSpecError),
/// Invalid string matcher
#[error(transparent)]
InvalidStringMatcher(#[from] StringMatcherParseError),
/// Invalid build number spec
#[error("invalid build number spec: {0}")]
InvalidBuildNumber(#[from] ParseBuildNumberSpecError),
/// Unable to parse hash digest from hex
#[error("Unable to parse hash digest from hex")]
InvalidHashDigest,
/// The package name was invalid
#[error(transparent)]
InvalidPackageName(#[from] InvalidPackageNameError),
}
impl FromStr for MatchSpec {
type Err = ParseMatchSpecError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_str(s, ParseStrictness::Lenient)
}
}
impl MatchSpec {
/// Parses a [`MatchSpec`] from a string with a given strictness.
pub fn from_str(
source: &str,
strictness: ParseStrictness,
) -> Result<Self, ParseMatchSpecError> {
matchspec_parser(source, strictness)
}
}
/// Strips a comment from a match spec. A comment is preceded by a '#' followed
/// by the comment itself. This functions splits the matchspec into the
/// matchspec and comment part.
fn strip_comment(input: &str) -> (&str, Option<&str>) {
input
.split_once('#')
.map_or_else(|| (input, None), |(spec, comment)| (spec, Some(comment)))
}
/// Strips any if statements from the matchspec. `if` statements in matchspec
/// are "anticipating future compatibility issues".
fn strip_if(input: &str) -> (&str, Option<&str>) {
// input
// .split_once("if")
// .map(|(spec, if_statement)| (spec, Some(if_statement)))
// .unwrap_or_else(|| (input, None))
(input, None)
}
/// An optimized data structure to store key value pairs in between a bracket
/// string `[key1=value1, key2=value2]`. The optimization stores two such values
/// on the stack and otherwise allocates a vector on the heap. Two is chosen
/// because that seems to be more than enough for most use cases.
type BracketVec<'a> = SmallVec<[(&'a str, &'a str); 2]>;
/// A parse combinator to filter whitespace if front and after another parser.
fn whitespace_enclosed<'a, F, O, E: ParseError<&'a str>>(
mut inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: FnMut(&'a str) -> IResult<&'a str, O, E>,
{
move |input: &'a str| {
let (input, _) = multispace0(input)?;
let (input, o2) = inner(input)?;
multispace0(input).map(|(i, _)| (i, o2))
}
}
/// Parses the contents of a bracket list `[version="1,2,3", bla=3]`
fn parse_bracket_list(input: &str) -> Result<BracketVec<'_>, ParseMatchSpecError> {
/// Parses a key in a bracket string
fn parse_key(input: &str) -> IResult<&str, &str> {
whitespace_enclosed(context(
"key",
take_while(|c: char| c.is_alphanumeric() || c == '_' || c == '-'),
))(input)
}
/// Parses a value in a bracket string.
fn parse_value(input: &str) -> IResult<&str, &str> {
whitespace_enclosed(context(
"value",
alt((
delimited(char('"'), take_until("\""), char('"')),
delimited(char('\''), take_until("'"), char('\'')),
take_till1(|c| c == ',' || c == ']' || c == '\'' || c == '"'),
)),
))(input)
}
/// Parses a `key=value` pair
fn parse_key_value(input: &str) -> IResult<&str, (&str, &str)> {
separated_pair(parse_key, char('='), parse_value)(input)
}
/// Parses a list of `key=value` pairs seperate by commas
fn parse_key_value_list(input: &str) -> IResult<&str, Vec<(&str, &str)>> {
separated_list0(whitespace_enclosed(char(',')), parse_key_value)(input)
}
/// Parses an entire bracket string
fn parse_bracket_list(input: &str) -> IResult<&str, Vec<(&str, &str)>> {
delimited(char('['), parse_key_value_list, char(']'))(input)
}
match parse_bracket_list(input).finish() {
Ok((_remaining, values)) => Ok(values.into()),
Err(nom::error::Error { .. }) => Err(ParseMatchSpecError::InvalidBracket),
}
}
/// Strips the brackets part of the matchspec returning the rest of the
/// matchspec and the contents of the brackets as a `Vec<&str>`.
fn strip_brackets(input: &str) -> Result<(Cow<'_, str>, BracketVec<'_>), ParseMatchSpecError> {
if let Some(matches) = lazy_regex::regex!(r#".*(?:(\[.*\]))$"#).captures(input) {
let bracket_str = matches.get(1).unwrap().as_str();
let bracket_contents = parse_bracket_list(bracket_str)?;
let input = if let Some(input) = input.strip_suffix(bracket_str) {
Cow::Borrowed(input)
} else {
Cow::Owned(input.replace(bracket_str, ""))
};
Ok((input, bracket_contents))
} else {
Ok((input.into(), SmallVec::new()))
}
}
/// Parses a [`BracketVec`] into precise components
fn parse_bracket_vec_into_components(
bracket: BracketVec<'_>,
match_spec: NamelessMatchSpec,
strictness: ParseStrictness,
) -> Result<NamelessMatchSpec, ParseMatchSpecError> {
let mut match_spec = match_spec;
for elem in bracket {
let (key, value) = elem;
match key {
"version" => match_spec.version = Some(VersionSpec::from_str(value, strictness)?),
"build" => match_spec.build = Some(StringMatcher::from_str(value)?),
"build_number" => match_spec.build_number = Some(BuildNumberSpec::from_str(value)?),
"sha256" => {
match_spec.sha256 = Some(
parse_digest_from_hex::<Sha256>(value)
.ok_or(ParseMatchSpecError::InvalidHashDigest)?,
);
}
"md5" => {
match_spec.md5 = Some(
parse_digest_from_hex::<Md5>(value)
.ok_or(ParseMatchSpecError::InvalidHashDigest)?,
);
}
"fn" => match_spec.file_name = Some(value.to_string()),
_ => Err(ParseMatchSpecError::InvalidBracketKey(key.to_owned()))?,
}
}
Ok(match_spec)
}
/// Strip the package name from the input.
fn strip_package_name(
input: &str,
strictness: ParseStrictness,
) -> Result<(PackageName, &str), ParseMatchSpecError> {
// In lenient mode, version specifiers can directly follow the package name. In
// strict mode this is not allowed.
let (package_name, rest) = if strictness == Lenient {
let (rest, name) =
take_while1(|c: char| !c.is_whitespace() && !is_start_of_version_constraint(c))(
input.trim(),
)
.finish()
.map_err(|_err: nom::error::Error<_>| ParseMatchSpecError::MissingPackageName)?;
(name, rest)
} else {
input
.trim()
.split_once(|c: char| c.is_whitespace())
.unwrap_or((input, ""))
};
let trimmed_package_name = package_name.trim();
if trimmed_package_name.is_empty() {
return Err(ParseMatchSpecError::MissingPackageName);
}
Ok((PackageName::from_str(trimmed_package_name)?, rest.trim()))
}
/// Splits a string into version and build constraints.
fn split_version_and_build(
input: &str,
strictness: ParseStrictness,
) -> Result<(&str, Option<&str>), ParseMatchSpecError> {
fn parse_version_constraint_or_group<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, &'a str, E> {
alt((
delimited(tag("("), parse_version_group, tag(")")),
recognize_constraint,
))(input)
}
fn parse_version_group<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, &'a str, E> {
recognize(separated_list1(
whitespace_enclosed(one_of(",|")),
parse_version_constraint_or_group,
))(input)
}
// Special case handling of `=*`, `=1.2.3`, or `=1*`
fn parse_special_equality<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, &'a str, E> {
// Matches ".*" or "*" but not "."
let version_glob = terminated(opt(tag(".")), tag("*"));
// Matches a version followed by an optional version glob
let version_followed_by_glob = terminated(recognize_version(true), opt(version_glob));
// Just matches the glob operator ("*")
let just_star = tag("*");
recognize(preceded(
tag("="),
alt((version_followed_by_glob, just_star)),
))(input)
}
fn parse_version_and_build_seperator<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
strictness: ParseStrictness,
) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str, E> {
move |input: &'a str| {
if strictness == Lenient {
terminated(
alt((parse_special_equality, parse_version_group)),
opt(one_of(" =")),
)(input)
} else {
terminated(parse_version_group, space0)(input)
}
}
}
match parse_version_and_build_seperator(strictness)(input).finish() {
Ok((rest, version)) => {
let build_string = rest.trim();
// Check validity of the build string
if strictness == Strict
&& build_string.contains(|c: char| !c.is_alphanumeric() && c != '_' && c != '*')
{
return Err(ParseMatchSpecError::InvalidBuildString(
build_string.to_owned(),
));
}
Ok((
version.trim(),
build_string.is_empty().not().then_some(build_string),
))
}
Err(nom::error::VerboseError { .. }) => Err(ParseMatchSpecError::InvalidVersionAndBuild(
input.to_string(),
)),
}
}
impl FromStr for NamelessMatchSpec {
type Err = ParseMatchSpecError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
Self::from_str(input, ParseStrictness::Lenient)
}
}
impl NamelessMatchSpec {
/// Parses a [`NamelessMatchSpec`] from a string with a given strictness.
pub fn from_str(input: &str, strictness: ParseStrictness) -> Result<Self, ParseMatchSpecError> {
// Strip off brackets portion
let (input, brackets) = strip_brackets(input.trim())?;
let mut match_spec =
parse_bracket_vec_into_components(brackets, NamelessMatchSpec::default(), strictness)?;
// Get the version and optional build string
let input = input.trim();
if !input.is_empty() {
if input.find('[').is_some() {
return Err(ParseMatchSpecError::MultipleBracketSectionsNotAllowed);
}
let (version_str, build_str) = split_version_and_build(input, strictness)?;
let version_str = if version_str.find(char::is_whitespace).is_some() {
Cow::Owned(version_str.replace(char::is_whitespace, ""))
} else {
Cow::Borrowed(version_str)
};
// Under certum circumstances we strip the `=` part of the version string. See
// the function documentation for more info.
let version_str = optionally_strip_equals(&version_str, build_str, strictness);
// Parse the version spec
match_spec.version = Some(
VersionSpec::from_str(version_str.as_ref(), strictness)
.map_err(ParseMatchSpecError::InvalidVersionSpec)?,
);
if let Some(build) = build_str {
match_spec.build = Some(StringMatcher::from_str(build)?);
}
}
Ok(match_spec)
}
}
/// Parses a conda match spec.
/// This is based on: <https://github.com/conda/conda/blob/master/conda/models/match_spec.py#L569>
fn matchspec_parser(
input: &str,
strictness: ParseStrictness,
) -> Result<MatchSpec, ParseMatchSpecError> {
// Step 1. Strip '#' and `if` statement
let (input, _comment) = strip_comment(input);
let (input, _if_clause) = strip_if(input);
// 2.a Is the spec an url, parse it as an url
if parse_scheme(input).is_some() {
let url = Url::parse(input)?;
let archive = ArchiveIdentifier::try_from_url(&url);
let name = archive.and_then(|a| a.try_into().ok());
// TODO: This should also work without a proper name from the url filename
if name.is_none() {
return Err(ParseMatchSpecError::MissingPackageName);
}
return Ok(MatchSpec {
url: Some(url),
name,
..MatchSpec::default()
});
}
// 2.b Is the spec a path, parse it as an url
if is_path(input) {
let path = Utf8TypedPath::from(input);
let url = file_url::file_path_to_url(path)
.map_err(|_error| ParseMatchSpecError::InvalidPackagePathOrUrl)?;
let archive = ArchiveIdentifier::try_from_url(&url);
let name = archive.and_then(|a| a.try_into().ok());
// TODO: This should also work without a proper name from the url filename
if name.is_none() {
return Err(ParseMatchSpecError::MissingPackageName);
}
return Ok(MatchSpec {
url: Some(url),
name,
..MatchSpec::default()
});
}
// 3. Strip off brackets portion
let (input, brackets) = strip_brackets(input.trim())?;
let mut nameless_match_spec =
parse_bracket_vec_into_components(brackets, NamelessMatchSpec::default(), strictness)?;
// 4. Strip off parens portion
// TODO: What is this? I've never seen in
// 5. Strip of '::' channel and namespace
let mut input_split = input.split(':').fuse();
let (input, namespace, channel_str) = match (
input_split.next(),
input_split.next(),
input_split.next(),
input_split.next(),
) {
(Some(input), None, _, _) => (input, None, None),
(Some(namespace), Some(input), None, _) => (input, Some(namespace), None),
(Some(channel_str), Some(namespace), Some(input), None) => {
(input, Some(namespace), Some(channel_str))
}
_ => return Err(ParseMatchSpecError::InvalidNumberOfColons),
};
nameless_match_spec.namespace = namespace
.map(str::trim)
.filter(|namespace| !namespace.is_empty())
.map(ToOwned::to_owned)
.or(nameless_match_spec.namespace);
if let Some(channel_str) = channel_str {
let channel_config = ChannelConfig::default_with_root_dir(
std::env::current_dir().expect("Could not get current directory"),
);
if let Some((channel, subdir)) = channel_str.rsplit_once('/') {
nameless_match_spec.channel = Some(Channel::from_str(channel, &channel_config)?.into());
nameless_match_spec.subdir = Some(subdir.to_string());
} else {
nameless_match_spec.channel =
Some(Channel::from_str(channel_str, &channel_config)?.into());
}
}
// Step 6. Strip off the package name from the input
let (name, input) = strip_package_name(input, strictness)?;
let mut match_spec = MatchSpec::from_nameless(nameless_match_spec, Some(name));
// Step 7. Otherwise sort our version + build
let input = input.trim();
if !input.is_empty() {
if input.find('[').is_some() {
return Err(ParseMatchSpecError::MultipleBracketSectionsNotAllowed);
}
let (version_str, build_str) = split_version_and_build(input, strictness)?;
let version_str = if version_str.find(char::is_whitespace).is_some() {
Cow::Owned(version_str.replace(char::is_whitespace, ""))
} else {
Cow::Borrowed(version_str)
};
// Under certain circumstantances we strip the `=` or `==` parts of the version
// string. See the function for more info.
let version_str = optionally_strip_equals(&version_str, build_str, strictness);
// Parse the version spec
match_spec.version = Some(
VersionSpec::from_str(version_str.as_ref(), strictness)
.map_err(ParseMatchSpecError::InvalidVersionSpec)?,
);
if let Some(build) = build_str {
match_spec.build = Some(StringMatcher::from_str(build)?);
}
}
Ok(match_spec)
}
/// HERE BE DRAGONS!
///
/// In some circumstainces we strip the `=` or `==` parts of the version string.
/// This is for conda legacy reasons. This function implements that behavior and
/// returns the stripped/updated version.
///
/// Most of this is only done in lenient mode. In strict mode we don't do any of
/// this.
fn optionally_strip_equals<'a>(
version_str: &'a str,
build_str: Option<&str>,
strictness: ParseStrictness,
) -> Cow<'a, str> {
// If the version doesn't start with `=` then don't strip anything.
let Some(version_without_equals) = version_str.strip_prefix('=') else {
return version_str.into();
};
// If we are not in lenient mode then stop processing at this point. Any other
// special case parsing behavior is only part of lenient mode parsing.
if strictness != Lenient {
return version_str.into();
}
// In lenient mode we have special case handling of version strings that start
// with `==`. If the version starts with `==` and the build string is none
// we strip the `==` part.
//
// This results in versions like `==1.0.*.*` being parsed as `1.0.*`.
// `==1.0.*.*` is considered a regex pattern and therefor not supported, but
// `1.0.*.*` is parsed as `1.0.*` in lenient mode. This is all very
// confusing and weird and is therefor only enabled in lenient parsing mode.
if let (Some(version_without_equals_equals), None) =
(version_without_equals.strip_prefix('='), build_str)
{
return version_without_equals_equals.into();
}
// Check if this version is part of a grouping or not. E.g. `2|>3|=4`
let is_grouping = version_without_equals.contains(['=', ',', '|']);
if is_grouping {
// If the version string forms a group we leave it untouched.
return version_str.into();
}
// If the version is not part of a grouping and doesn't end in a `*` and there
// is no build string then we add a `*`.
if build_str.is_none() && !version_without_equals.ends_with('*') {
format!("{version_without_equals}*").into()
} else {
// Otherwise if it is not part of a group we simply remove the `=` without
// adding the `*`.
version_without_equals.into()
}
}
#[cfg(test)]
mod tests {
use std::{collections::BTreeMap, str::FromStr, sync::Arc};
use assert_matches::assert_matches;
use rattler_digest::{parse_digest_from_hex, Md5, Sha256};
use rstest::rstest;
use serde::Serialize;
use smallvec::smallvec;
use url::Url;
use super::{
split_version_and_build, strip_brackets, strip_package_name, BracketVec, MatchSpec,
ParseMatchSpecError,
};
use crate::{
match_spec::parse::parse_bracket_list, BuildNumberSpec, Channel, ChannelConfig,
NamelessMatchSpec, ParseStrictness, ParseStrictness::*, VersionSpec,
};
fn channel_config() -> ChannelConfig {
ChannelConfig::default_with_root_dir(
std::env::current_dir().expect("Could not get current directory"),
)
}
#[test]
fn test_strip_brackets() {
let result = strip_brackets(r#"bla [version="1.2.3"]"#).unwrap();
assert_eq!(result.0, "bla ");
let expected: BracketVec<'_> = smallvec![("version", "1.2.3")];
assert_eq!(result.1, expected);
let result = strip_brackets(r#"bla [version='1.2.3']"#).unwrap();
assert_eq!(result.0, "bla ");
let expected: BracketVec<'_> = smallvec![("version", "1.2.3")];
assert_eq!(result.1, expected);
let result = strip_brackets(r#"bla [version=1]"#).unwrap();
assert_eq!(result.0, "bla ");
let expected: BracketVec<'_> = smallvec![("version", "1")];
assert_eq!(result.1, expected);
let result = strip_brackets(r#"conda-forge::bla[version=1]"#).unwrap();
assert_eq!(result.0, "conda-forge::bla");
let expected: BracketVec<'_> = smallvec![("version", "1")];
assert_eq!(result.1, expected);
let result = strip_brackets(r#"bla [version="1.2.3", build_number=1]"#).unwrap();
assert_eq!(result.0, "bla ");
let expected: BracketVec<'_> = smallvec![("version", "1.2.3"), ("build_number", "1")];
assert_eq!(result.1, expected);
}
#[test]
fn test_split_version_and_build() {
assert_matches!(
split_version_and_build("2.7|>=3.6", Lenient),
Ok(("2.7|>=3.6", None))
);
assert_matches!(
split_version_and_build("==1.0=py27_0", Lenient),
Ok(("==1.0", Some("py27_0")))
);
assert_matches!(
split_version_and_build("=*=cuda", Lenient),
Ok(("=*", Some("cuda")))
);
assert_matches!(
split_version_and_build("=1.2.3 0", Lenient),
Ok(("=1.2.3", Some("0")))
);
assert_matches!(
split_version_and_build("1.2.3=0", Lenient),
Ok(("1.2.3", Some("0")))
);
assert_matches!(
split_version_and_build(">=1.0 , < 2.0 py34_0", Lenient),
Ok((">=1.0 , < 2.0", Some("py34_0")))
);
assert_matches!(
split_version_and_build(">=1.0 , < 2.0 =py34_0", Lenient),
Ok((">=1.0 , < 2.0", Some("=py34_0")))
);
assert_matches!(
split_version_and_build("=1.2.3 ", Lenient),
Ok(("=1.2.3", None))
);
assert_matches!(
split_version_and_build(">1.8,<2|==1.7", Lenient),
Ok((">1.8,<2|==1.7", None))
);
assert_matches!(
split_version_and_build("* openblas_0", Lenient),
Ok(("*", Some("openblas_0")))
);
assert_matches!(
split_version_and_build("* *", Lenient),
Ok(("*", Some("*")))
);
assert_matches!(
split_version_and_build(">=1!164.3095,<1!165", Lenient),
Ok((">=1!164.3095,<1!165", None))
);
assert_matches!(
split_version_and_build("==1!164.3095,<1!165=py27_0", Strict),
Err(ParseMatchSpecError::InvalidBuildString(_))
);
assert_matches!(
split_version_and_build("3.8.* *_cpython", Lenient),
Ok(("3.8.*", Some("*_cpython")))
);
assert_matches!(
split_version_and_build("3.8.* *_cpython", Strict),
Ok(("3.8.*", Some("*_cpython")))
);
}
#[test]
fn test_nameless_match_spec() {
insta::assert_yaml_snapshot!([
NamelessMatchSpec::from_str("3.8.* *_cpython", Strict).unwrap(),
NamelessMatchSpec::from_str("1.0 py27_0[fn=\"bla\"]", Strict).unwrap(),
NamelessMatchSpec::from_str("=1.0 py27_0", Strict).unwrap(),
],
@r###"
---
- version: 3.8.*
build: "*_cpython"
- version: "==1.0"
build: py27_0
file_name: bla
- version: 1.0.*
build: py27_0
"###);
}
#[test]
fn test_match_spec_more() {
let spec = MatchSpec::from_str("conda-forge::foo[version=\"1.0.*\"]", Strict).unwrap();
assert_eq!(spec.name, Some("foo".parse().unwrap()));
assert_eq!(
spec.version,
Some(VersionSpec::from_str("1.0.*", Strict).unwrap())
);
assert_eq!(
spec.channel,
Some(
Channel::from_str("conda-forge", &channel_config())
.map(Arc::new)
.unwrap()
)
);
let spec = MatchSpec::from_str("conda-forge::foo[version=1.0.*]", Strict).unwrap();
assert_eq!(spec.name, Some("foo".parse().unwrap()));
assert_eq!(
spec.version,
Some(VersionSpec::from_str("1.0.*", Strict).unwrap())
);
assert_eq!(
spec.channel,
Some(
Channel::from_str("conda-forge", &channel_config())
.map(Arc::new)
.unwrap()
)
);
let spec = MatchSpec::from_str(
r#"conda-forge::foo[version=1.0.*, build_number=">6"]"#,
Strict,
)
.unwrap();
assert_eq!(spec.name, Some("foo".parse().unwrap()));
assert_eq!(
spec.version,
Some(VersionSpec::from_str("1.0.*", Strict).unwrap())
);
assert_eq!(
spec.channel,
Some(
Channel::from_str("conda-forge", &channel_config())
.map(Arc::new)
.unwrap()
)
);
assert_eq!(
spec.build_number,
Some(BuildNumberSpec::from_str(">6").unwrap())
);
}
#[test]
fn test_hash_spec() {
let spec = MatchSpec::from_str("conda-forge::foo[md5=1234567890]", Strict);
assert_matches!(spec, Err(ParseMatchSpecError::InvalidHashDigest));
let spec = MatchSpec::from_str("conda-forge::foo[sha256=1234567890]", Strict);
assert_matches!(spec, Err(ParseMatchSpecError::InvalidHashDigest));
let spec = MatchSpec::from_str("conda-forge::foo[sha256=315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3]", Strict).unwrap();
assert_eq!(
spec.sha256,
Some(
parse_digest_from_hex::<Sha256>(
"315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3"
)
.unwrap()
)
);
let spec = MatchSpec::from_str(
"conda-forge::foo[md5=8b1a9953c4611296a827abf8c47804d7]",
Strict,
)
.unwrap();
assert_eq!(
spec.md5,
Some(parse_digest_from_hex::<Md5>("8b1a9953c4611296a827abf8c47804d7").unwrap())
);
}
#[test]
fn test_parse_bracket_list() {
assert_eq!(
parse_bracket_list("[version=1.0.1]").unwrap().as_ref(),
&[("version", "1.0.1")]
);
assert_eq!(
parse_bracket_list("[version='1.0.1']").unwrap().as_ref(),
&[("version", "1.0.1")]
);
assert_eq!(
parse_bracket_list("[version=\"1.0.1\"]").unwrap().as_ref(),
&[("version", "1.0.1")]
);
assert_eq!(
parse_bracket_list("[version=1.0.1, build=3]")
.unwrap()
.as_ref(),
&[("version", "1.0.1"), ("build", "3")]
);
assert_eq!(
parse_bracket_list("[version='1.0.1', build=3]")
.unwrap()
.as_ref(),
&[("version", "1.0.1"), ("build", "3")]
);
assert_eq!(
parse_bracket_list("[version=\"1.0.1\", build=3]")
.unwrap()
.as_ref(),
&[("version", "1.0.1"), ("build", "3")]
);
assert_eq!(
parse_bracket_list("[build=\"py2*\"]").unwrap().as_ref(),
&[("build", "py2*")]
);
assert_eq!(
parse_bracket_list("[build=py2*]").unwrap().as_ref(),
&[("build", "py2*")]
);
assert_eq!(
parse_bracket_list("[version=\"1.3,2.0\"]")
.unwrap()
.as_ref(),
&[("version", "1.3,2.0")]
);
}
#[rstest]
#[case::lenient(Lenient)]
#[case::strict(Strict)]
fn test_from_str(#[case] strictness: ParseStrictness) {
#[derive(Serialize)]
#[serde(untagged)]
enum MatchSpecOrError {
Error { error: String },
MatchSpec(MatchSpec),
}
// A list of matchspecs to parse.
// Please keep this list sorted.
let specs = [
"blas *.* mkl",
"C:\\Users\\user\\conda-bld\\linux-64\\foo-1.0-py27_0.tar.bz2",
"foo=1.0=py27_0",
"foo==1.0=py27_0",
"https://conda.anaconda.org/conda-forge/linux-64/py-rattler-0.6.1-py39h8169da8_0.conda",
"https://repo.prefix.dev/ruben-arts/linux-64/boost-cpp-1.78.0-h75c5d50_1.tar.bz2",
"python 3.8.* *_cpython",
"pytorch=*=cuda*",
"x264 >=1!164.3095,<1!165",
"/home/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2",
"conda-forge::foo[version=1.0.*]",
"conda-forge::foo[version=1.0.*, build_number=\">6\"]",
"python ==2.7.*.*|>=3.6",
"python=3.9",
];
let evaluated: BTreeMap<_, _> = specs
.iter()
.map(|spec| {
(
spec,
MatchSpec::from_str(spec, strictness).map_or_else(
|err| MatchSpecOrError::Error {
error: err.to_string(),
},
MatchSpecOrError::MatchSpec,
),
)
})
.collect();
insta::assert_yaml_snapshot!(format!("test_from_string_{strictness:?}"), evaluated);
}
#[rstest]
#[case::lenient(Lenient)]
#[case::strict(Strict)]
fn test_nameless_from_str(#[case] strictness: ParseStrictness) {
#[derive(Serialize)]
#[serde(untagged)]
enum NamelessSpecOrError {
Error { error: String },
Spec(NamelessMatchSpec),
}
// A list of matchspecs to parse.
// Please keep this list sorted.
let specs = ["2.7|>=3.6"];
let evaluated: BTreeMap<_, _> = specs
.iter()
.map(|spec| {
(
spec,
NamelessMatchSpec::from_str(spec, strictness).map_or_else(
|err| NamelessSpecOrError::Error {
error: err.to_string(),
},
NamelessSpecOrError::Spec,
),
)
})
.collect();
insta::assert_yaml_snapshot!(
format!("test_nameless_from_string_{strictness:?}"),
evaluated
);
}
#[test]
fn test_invalid_bracket() {
assert_matches!(
strip_brackets(r#"bla [version="1.2.3", build_number=]"#),
Err(ParseMatchSpecError::InvalidBracket)
);
assert_matches!(
strip_brackets(r#"bla [version="1.2.3, build_number=1]"#),
Err(ParseMatchSpecError::InvalidBracket)
);
}
#[test]
fn test_invalid_bracket_key() {
let _unknown_key = String::from("unknown");
let spec = MatchSpec::from_str("conda-forge::foo[unknown=1.0.*]", Strict);
assert_matches!(
spec,
Err(ParseMatchSpecError::InvalidBracketKey(_unknown_key))
);
}
#[test]
fn test_invalid_number_of_colons() {
let spec = MatchSpec::from_str("conda-forge::::foo[version=\"1.0.*\"]", Strict);
assert_matches!(spec, Err(ParseMatchSpecError::InvalidNumberOfColons));
}
#[test]
fn test_missing_package_name() {
let package_name = strip_package_name("", Lenient);
assert_matches!(package_name, Err(ParseMatchSpecError::MissingPackageName));
}
#[test]
fn test_empty_namespace() {