Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(defaults): switch to a default that is defined everywhere #15

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ testutils = { path = "./tests/testutils" }
test-log = {version = "0.2.13", default-features = false, features = ["trace"]}
pretty_assertions = "1.4.0"
pretty_assertions_sorted = "1.2.3"
glob = "0.3.1"
47 changes: 33 additions & 14 deletions src/tasks/defaults/plist_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ Working out the rules for preferences was fairly complex, but if you run `defaul

As far as I can tell, the rules are:

- `NSGlobalDomain` -> `~/Library/Preferences/.GlobalPreferences.plist`
- `~/Library/Containers/{domain}/Data/Library/Preferences/{domain}.plist` if it exists.
- `~/Library/Preferences/{domain}.plist`
### Normal Preferences

- if `NSGlobalDomain` -> `~/Library/Preferences/.GlobalPreferences.plist`
- if file exists -> `~/Library/Containers/{domain}/Data/Library/Preferences/{domain}.plist`
- else -> `~/Library/Preferences/{domain}.plist`

If none of these exist then create `~/Library/Preferences/{domain}.plist`.

Note that `defaults domains` actually prints out `~/Library/Containers/{*}/Data/Library/Preferences/{*}.plist` (i.e. any plist file name inside a container folder), but `defaults read` only actually checks `~/Library/Containers/{domain}/Data/Library/Preferences/{domain}.plist` (a plist file whose name matches the container folder.
Note that `defaults domains` actually prints out `~/Library/Containers/{*}/Data/Library/Preferences/{*}.plist` (i.e. any plist file name inside a container folder), but `defaults read` only actually checks `~/Library/Containers/{domain}/Data/Library/Preferences/{domain}.plist` (a plist file whose name matches the container folder).

### `CurrentHost` / `ByHost` Preferences

- if `NSGlobalDomain` -> `~/Library/Preferences/ByHost/.GlobalPreferences.{hardware_uuid}.plist`
- if file exists -> `~/Library/Containers/{domain}/Data/Library/Preferences/ByHost/{domain}.{hardware_uuid}.plist` if it exists.
- else -> `~/Library/Preferences/ByHost/{domain}.{hardware_uuid}.plist`

### Useful Resources

Expand Down Expand Up @@ -382,30 +390,29 @@ fn replace_ellipsis_dict(new_value: &mut plist::Value, old_value: Option<&plist:
#[cfg(test)]
mod tests {
use crate::utils::mac;
use camino::Utf8PathBuf;
use serial_test::serial;

#[test]
#[serial(home_dir)] // Test relies on or changes the $HOME env var.
fn plist_path_tests() {
let home_dir = Utf8PathBuf::try_from(dirs::home_dir().unwrap()).unwrap();

{
let domain_path = super::plist_path("NSGlobalDomain", false).unwrap();
assert_eq!(
dirs::home_dir()
.unwrap()
.join("Library/Preferences/.GlobalPreferences.plist"),
home_dir.join("Library/Preferences/.GlobalPreferences.plist"),
domain_path
);
}

{
let mut expected_plist_path = dirs::home_dir().unwrap().join(
let mut expected_plist_path = home_dir.join(
"Library/Containers/com.apple.Safari/Data/Library/Preferences/com.apple.Safari.\
plist",
);
if !expected_plist_path.exists() {
expected_plist_path = dirs::home_dir()
.unwrap()
.join("Library/Preferences/com.apple.Safari.plist");
expected_plist_path = home_dir.join("Library/Preferences/com.apple.Safari.plist");
}
let domain_path = super::plist_path("com.apple.Safari", false).unwrap();
assert_eq!(expected_plist_path, domain_path);
Expand All @@ -416,7 +423,7 @@ mod tests {
let domain_path = super::plist_path("NSGlobalDomain", true).unwrap();
let hardware_uuid = mac::get_hardware_uuid().unwrap();
assert_eq!(
dirs::home_dir().unwrap().join(format!(
home_dir.join(format!(
"Library/Preferences/ByHost/.GlobalPreferences.{hardware_uuid}.plist"
)),
domain_path
Expand All @@ -428,11 +435,23 @@ mod tests {
let domain_path = super::plist_path("com.apple.Safari", true).unwrap();
let hardware_uuid = mac::get_hardware_uuid().unwrap();
assert_eq!(
dirs::home_dir().unwrap().join(format!(
home_dir.join(format!(
"Library/Containers/com.apple.Safari/Data/Library/Preferences/ByHost/com.\
apple.Safari.{hardware_uuid}.plist"
)),
domain_path
domain_path,
"Failed to find expected sandboxed plist file for Safari. Other possible matches: \
{glob_matches:#?}",
glob_matches = glob::glob(
home_dir
.join(format!(
"Library/Containers/*/Data/Library/Preferences/ByHost/*.\
{hardware_uuid}.plist"
))
.as_str()
)
.unwrap()
.collect::<Vec<_>>(),
);
}
}
Expand Down
31 changes: 8 additions & 23 deletions tests/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,24 @@ fn test_defaults_read_global() {
fn test_defaults_read_local() {
let temp_dir = testutils::temp_dir("up", testutils::function_path!()).unwrap();

// Four-letter codes for view modes: `icnv`, `clmv`, `glyv`, `Nlsv`
let mut expected_value = cmd!(
"defaults",
"read",
"com.apple.finder",
"FXPreferredViewStyle"
)
.read()
.unwrap();
// Dock region, e.g. 'GB'
let mut expected_value = cmd!("defaults", "read", "com.apple.dock", "region")
.read()
.unwrap();
expected_value.push('\n');

// Reading a normal value should have the same output as the defaults command (but yaml not
// defaults own format).
{
let mut cmd = testutils::test_binary_cmd("up", &temp_dir);
cmd.args([
"defaults",
"read",
"com.apple.finder",
"FXPreferredViewStyle",
]);
cmd.args(["defaults", "read", "com.apple.dock", "region"]);
cmd.assert().success().stdout(expected_value.clone());
}

// A .plist extension should be allowed too.
{
let mut cmd = testutils::test_binary_cmd("up", &temp_dir);
cmd.args([
"defaults",
"read",
"com.apple.finder.plist",
"FXPreferredViewStyle",
]);
cmd.args(["defaults", "read", "com.apple.dock.plist", "region"]);
cmd.assert().success().stdout(expected_value.clone());
}

Expand All @@ -98,10 +83,10 @@ fn test_defaults_read_local() {
"defaults",
"read",
&format!(
"{}/Library/Preferences/com.apple.finder.plist",
"{}/Library/Preferences/com.apple.dock.plist",
dirs::home_dir().unwrap().display()
),
"FXPreferredViewStyle",
"region",
]);
cmd.assert().success().stdout(expected_value);
}
Expand Down
Loading