Skip to content

Commit 7838e20

Browse files
committed
Fix tests
1 parent 6844e46 commit 7838e20

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

crates/bindings/tmc-langs-node/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! lock {
99
( $cx: ident, $( $path: expr ),+ ) => {
1010
$(
1111
let path_buf: PathBuf = (&$path).into();
12-
let mut lock = $crate::file_util::Lock::file(path_buf, $crate::file_util::LockOptions::Write).map_err(|e| $crate::helpers::convert_err(&mut $cx, e))?;
12+
let mut lock = $crate::file_util::Lock::dir(path_buf, $crate::file_util::LockOptions::Write).map_err(|e| $crate::helpers::convert_err(&mut $cx, e))?;
1313
let _guard = lock.lock().map_err(|e| $crate::helpers::convert_err(&mut $cx, e))?;
1414
)*
1515
};

crates/tmc-langs/src/config/tmc_config.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,44 +39,30 @@ impl TmcConfig {
3939
/// Reads or initialises for the client from the given path.
4040
pub fn load_from(client_name: &str, path: PathBuf) -> Result<TmcConfig, LangsError> {
4141
// try to open config file
42-
let lock = Lock::file(&path, LockOptions::Read);
43-
let config = match lock {
44-
Ok(mut lock) => {
45-
// found config file, lock and read
46-
let mut buf = String::new();
47-
let _bytes = lock
48-
.lock()?
49-
.get_file()
50-
.read_to_string(&mut buf)
51-
.map_err(|e| FileError::FileRead(path.clone(), e))?;
52-
match deserialize::toml_from_str::<Self>(&buf) {
53-
// successfully read file, try to deserialize
54-
Ok(mut config) => {
55-
// set the path which was set to default during deserialization
56-
config.location = path;
57-
config // successfully read and deserialized the config
58-
}
59-
Err(e) => {
60-
log::error!(
61-
"Failed to deserialize config at {} due to {}, resetting",
62-
path.display(),
63-
e
64-
);
65-
drop(lock); // unlock file before recreating it
66-
Self::init_at(client_name, path)?
67-
}
42+
let config = if path.exists() {
43+
// found config file
44+
let data = file_util::read_file_to_string(&path)?;
45+
match deserialize::toml_from_str::<Self>(&data) {
46+
// successfully read file, try to deserialize
47+
Ok(mut config) => {
48+
// set the path which was set to default during deserialization
49+
config.location = path;
50+
config // successfully read and deserialized the config
51+
}
52+
Err(e) => {
53+
log::error!(
54+
"Failed to deserialize config at {} due to {}, resetting",
55+
path.display(),
56+
e
57+
);
58+
Self::init_at(client_name, path)?
6859
}
6960
}
70-
Err(e) => {
71-
// failed to open config file, create new one
72-
log::info!(
73-
"could not open config file at {} due to {}, initializing a new config file",
74-
path.display(),
75-
e
76-
);
77-
// todo: check the cause to make sure this makes sense, might be necessary to propagate some error kinds
78-
Self::init_at(client_name, path)?
79-
}
61+
} else {
62+
// failed to open config file, create new one
63+
log::info!("initializing a new config file at {}", path.display());
64+
// todo: check the cause to make sure this makes sense, might be necessary to propagate some error kinds
65+
Self::init_at(client_name, path)?
8066
};
8167

8268
if !config.projects_dir.exists() {

0 commit comments

Comments
 (0)