Skip to content

Commit f29e1ba

Browse files
test: add basic test to validate that we can parse .netrc files properly (#503)
1 parent 23dc2ba commit f29e1ba

File tree

1 file changed

+63
-0
lines changed
  • crates/rattler_networking/src/authentication_storage/backends

1 file changed

+63
-0
lines changed

crates/rattler_networking/src/authentication_storage/backends/netrc.rs

+63
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,66 @@ impl StorageBackend for NetRcStorage {
9898
}
9999
}
100100
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
use std::io::Write;
106+
use tempfile::tempdir;
107+
108+
#[test]
109+
fn test_file_storage() {
110+
let file = tempdir().unwrap();
111+
let path = file.path().join(".testnetrc");
112+
113+
let mut netrc = std::fs::File::create(&path).unwrap();
114+
netrc
115+
.write_all(b"machine test\nlogin test\npassword password\n")
116+
.unwrap();
117+
netrc.flush().unwrap();
118+
119+
let storage = NetRcStorage::from_path(path.as_path()).unwrap();
120+
assert_eq!(
121+
storage.get("test").unwrap(),
122+
Some(Authentication::BasicHTTP {
123+
username: "test".to_string(),
124+
password: "password".to_string(),
125+
})
126+
);
127+
128+
assert_eq!(storage.get("test_unknown").unwrap(), None);
129+
}
130+
131+
#[test]
132+
fn test_file_storage_from_env() {
133+
let file = tempdir().unwrap();
134+
let path = file.path().join(".testnetrc2");
135+
136+
let mut netrc = std::fs::File::create(&path).unwrap();
137+
netrc
138+
.write_all(b"machine test2\nlogin test2\npassword password2\n")
139+
.unwrap();
140+
netrc.flush().unwrap();
141+
142+
let old_netrc = env::var("NETRC");
143+
env::set_var("NETRC", path.as_os_str());
144+
145+
let storage = NetRcStorage::from_env().unwrap();
146+
147+
assert_eq!(
148+
storage.get("test2").unwrap(),
149+
Some(Authentication::BasicHTTP {
150+
username: "test2".to_string(),
151+
password: "password2".to_string(),
152+
})
153+
);
154+
155+
assert_eq!(storage.get("test_unknown").unwrap(), None);
156+
157+
if let Ok(netrc) = old_netrc {
158+
env::set_var("NETRC", netrc);
159+
} else {
160+
env::remove_var("NETRC");
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)