Skip to content

Commit

Permalink
fix: add snapshot test and use btreemap in file backend (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Feb 26, 2024
1 parent 1c94bc2 commit a008932
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use anyhow::Result;
use fslock::LockFile;
use once_cell::sync::Lazy;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, HashSet};
use std::{path::PathBuf, sync::Mutex};

use crate::authentication_storage::StorageBackend;
Expand Down Expand Up @@ -60,9 +60,9 @@ impl FileStorage {
Ok(lock)
}

/// Read the JSON file and deserialize it into a `HashMap`, or return an empty `HashMa`p if the
/// Read the JSON file and deserialize it into a `BTreeMap`, or return an empty `BTreeMap` if the
/// file does not exist
fn read_json(&self) -> Result<HashMap<String, Authentication>, FileStorageError> {
fn read_json(&self) -> Result<BTreeMap<String, Authentication>, FileStorageError> {
if !self.path.exists() {
static WARN_GUARD: Lazy<Mutex<HashSet<PathBuf>>> =
Lazy::new(|| Mutex::new(HashSet::new()));
Expand All @@ -73,16 +73,16 @@ impl FileStorage {
self.path.to_string_lossy()
);
}
return Ok(HashMap::new());
return Ok(BTreeMap::new());
}
let file = std::fs::File::open(&self.path)?;
let reader = std::io::BufReader::new(file);
let dict = serde_json::from_reader(reader)?;
Ok(dict)
}

/// Serialize the given `HashMap` and write it to the JSON file
fn write_json(&self, dict: &HashMap<String, Authentication>) -> Result<(), FileStorageError> {
/// Serialize the given `BTreeMap` and write it to the JSON file
fn write_json(&self, dict: &BTreeMap<String, Authentication>) -> Result<(), FileStorageError> {
let file = std::fs::File::create(&self.path)?;
let writer = std::io::BufWriter::new(file);
serde_json::to_writer(writer, dict)?;
Expand Down Expand Up @@ -124,7 +124,8 @@ impl Default for FileStorage {
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use insta::assert_snapshot;
use std::{fs, io::Write};
use tempfile::tempdir;

#[test]
Expand All @@ -144,6 +145,24 @@ mod tests {
Some(Authentication::CondaToken("password".to_string()))
);

storage
.store(
"bearer",
&Authentication::BearerToken("password".to_string()),
)
.unwrap();
storage
.store(
"basic",
&Authentication::BasicHTTP {
username: "user".to_string(),
password: "password".to_string(),
},
)
.unwrap();

assert_snapshot!(fs::read_to_string(&path).unwrap());

storage.delete("test").unwrap();
assert_eq!(storage.get("test").unwrap(), None);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/rattler_networking/src/authentication_storage/backends/file.rs
expression: "fs::read_to_string(&path).unwrap()"
---
{"basic":{"BasicHTTP":{"username":"user","password":"password"}},"bearer":{"BearerToken":"password"},"test":{"CondaToken":"password"}}

0 comments on commit a008932

Please sign in to comment.