-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_version_file.rs
More file actions
208 lines (200 loc) · 6.73 KB
/
python_version_file.rs
File metadata and controls
208 lines (200 loc) · 6.73 KB
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
use crate::python_version::{PythonVersionOrigin, RequestedPythonVersion};
/// Parse the contents of a `.python-version` file into a [`RequestedPythonVersion`].
///
/// The file is expected to contain a string of form `X.Y` or `X.Y.Z`. Leading and trailing
/// whitespace will be removed from each line. Lines which are either comments (that begin
/// with `#`) or are empty will be ignored. Multiple Python versions are not permitted.
pub(crate) fn parse(contents: &str) -> Result<RequestedPythonVersion, ParsePythonVersionFileError> {
let versions = contents
.lines()
.filter_map(|line| {
let trimmed_line = line.trim();
if trimmed_line.is_empty() || trimmed_line.starts_with('#') {
None
} else {
// ASCII control characters and Unicode are replaced to make the error messages
// easier to understand when non-visible characters are present in the file.
// We can't use `escape_default()` here because it also escapes quotes.
Some(trimmed_line.replace(|c: char| c.is_ascii_control() || !c.is_ascii(), "�"))
}
})
.collect::<Vec<String>>();
match versions.as_slice() {
[version] => match version
.split('.')
.map(str::parse)
.collect::<Result<Vec<u16>, _>>()
.unwrap_or_default()[..]
{
[major, minor, patch] => Ok(RequestedPythonVersion {
major,
minor,
patch: Some(patch),
origin: PythonVersionOrigin::PythonVersionFile,
}),
[major, minor] => Ok(RequestedPythonVersion {
major,
minor,
patch: None,
origin: PythonVersionOrigin::PythonVersionFile,
}),
_ => Err(ParsePythonVersionFileError::InvalidVersion(version.clone())),
},
[] => Err(ParsePythonVersionFileError::NoVersion),
_ => Err(ParsePythonVersionFileError::MultipleVersions(versions)),
}
}
/// Errors that can occur when parsing the contents of a `.python-version` file.
#[derive(Debug, PartialEq)]
pub(crate) enum ParsePythonVersionFileError {
InvalidVersion(String),
MultipleVersions(Vec<String>),
NoVersion,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_valid() {
assert_eq!(
parse("1.2"),
Ok(RequestedPythonVersion {
major: 1,
minor: 2,
patch: None,
origin: PythonVersionOrigin::PythonVersionFile,
})
);
assert_eq!(
parse("987.654.3210"),
Ok(RequestedPythonVersion {
major: 987,
minor: 654,
patch: Some(3210),
origin: PythonVersionOrigin::PythonVersionFile,
})
);
assert_eq!(
parse("1.2\n"),
Ok(RequestedPythonVersion {
major: 1,
minor: 2,
patch: None,
origin: PythonVersionOrigin::PythonVersionFile,
})
);
assert_eq!(
parse(" # Comment 1\n\n \t 1.2.3 \r\n # Comment 2"),
Ok(RequestedPythonVersion {
major: 1,
minor: 2,
patch: Some(3),
origin: PythonVersionOrigin::PythonVersionFile,
})
);
}
#[test]
fn parse_invalid_version() {
assert_eq!(
parse("1"),
Err(ParsePythonVersionFileError::InvalidVersion("1".to_string()))
);
assert_eq!(
parse("1.2.3.4"),
Err(ParsePythonVersionFileError::InvalidVersion(
"1.2.3.4".to_string()
))
);
assert_eq!(
parse("1..3"),
Err(ParsePythonVersionFileError::InvalidVersion(
"1..3".to_string()
))
);
assert_eq!(
parse("1.2.3."),
Err(ParsePythonVersionFileError::InvalidVersion(
"1.2.3.".to_string()
))
);
assert_eq!(
parse("1.2rc1"),
Err(ParsePythonVersionFileError::InvalidVersion(
"1.2rc1".to_string()
))
);
assert_eq!(
parse("1.2.3-dev"),
Err(ParsePythonVersionFileError::InvalidVersion(
"1.2.3-dev".to_string()
))
);
// We don't support the `python-` prefix form since it's undocumented and will likely
// be deprecated: https://github.com/pyenv/pyenv/issues/3054#issuecomment-2341316638
assert_eq!(
parse("python-1.2.3"),
Err(ParsePythonVersionFileError::InvalidVersion(
"python-1.2.3".to_string()
))
);
assert_eq!(
parse("system"),
Err(ParsePythonVersionFileError::InvalidVersion(
"system".to_string()
))
);
assert_eq!(
parse(" # Comment 1\n ' 1 2 3 ' \n # Comment 2"),
Err(ParsePythonVersionFileError::InvalidVersion(
"' 1 2 3 '".to_string()
))
);
// ASCII control character `ESC`.
assert_eq!(
parse("3.12\u{1b}"),
Err(ParsePythonVersionFileError::InvalidVersion(
"3.12�".to_string()
))
);
// Extended ASCII soft hyphen.
assert_eq!(
parse("\u{ad}3.12"),
Err(ParsePythonVersionFileError::InvalidVersion(
"�3.12".to_string()
))
);
// Unicode zero width no-break space.
assert_eq!(
parse("\u{feff}3.12\u{feff}"),
Err(ParsePythonVersionFileError::InvalidVersion(
"�3.12�".to_string()
))
);
}
#[test]
fn parse_no_version() {
assert_eq!(parse(""), Err(ParsePythonVersionFileError::NoVersion));
assert_eq!(parse("\n"), Err(ParsePythonVersionFileError::NoVersion));
assert_eq!(
parse("# Comment 1\n \n # Comment 2"),
Err(ParsePythonVersionFileError::NoVersion)
);
}
#[test]
fn parse_multiple_versions() {
assert_eq!(
parse("1.2\n3.4"),
Err(ParsePythonVersionFileError::MultipleVersions(vec![
"1.2".to_string(),
"3.4".to_string()
]))
);
assert_eq!(
parse(" # Comment 1\n 1.2 \n # Comment 2\n\t'python-\u{ad}3.4'"),
Err(ParsePythonVersionFileError::MultipleVersions(vec![
"1.2".to_string(),
"'python-�3.4'".to_string()
]))
);
}
}