Debian Codegraph @

lib/tests/test_local_working_copy_executable_bit.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #![cfg(unix)] // Nothing to test on Windows
16
17 use std::fs;
18 use std::os::unix::fs::PermissionsExt;
19 use std::path::Path;
20 use std::sync::Arc;
21
22 use jj_lib::backend::TreeValue;
23 use jj_lib::commit::Commit;
24 use jj_lib::merged_tree::MergedTree;
25 use jj_lib::repo::Repo as _;
26 use jj_lib::repo_path::RepoPath;
27 use jj_lib::store::Store;
28 use pollster::FutureExt as _;
29 use testutils::TestResult;
30 use testutils::TestTreeBuilder;
31 use testutils::TestWorkspace;
32 use testutils::repo_path;
33
34 /// Assert that a file's executable bit matches the expected value.
35 #[track_caller]
36 fn assert_file_executable(path: &Path, expected: bool) {
37 let perms = path.metadata().unwrap().permissions();
38 let actual = (perms.mode() & 0o100) == 0o100;
39 assert_eq!(actual, expected);
40 }
41
42 /// Set the executable bit of a file on the filesystem.
43 #[track_caller]
44 fn set_file_executable(path: &Path, executable: bool) {
45 let prev_mode = path.metadata().unwrap().permissions().mode();
46 let is_executable = prev_mode & 0o100 != 0;
47 assert_ne!(executable, is_executable, "why are you calling this?");
48 let new_mode = if executable { 0o755 } else { 0o644 };
49 fs::set_permissions(path, PermissionsExt::from_mode(new_mode)).unwrap();
50 }
51
52 /// Assert that a tree value's executable bit matches the expected value.
53 #[track_caller]
54 fn assert_tree_executable(tree_val: TreeValue, expected: bool) {
55 if let TreeValue::File { executable, .. } = tree_val {
56 assert_eq!(executable, expected);
57 } else {
58 panic!()
59 }
60 }
61
62 /// Create a tree with an empty file having the given executable bit. Returns
63 /// the new tree id.
64 #[track_caller]
65 fn create_tree_executable(
66 store: &Arc<Store>,
67 repo_path: &RepoPath,
68 executable: bool,
69 ) -> MergedTree {
70 let mut tree_builder = TestTreeBuilder::new(store.clone());
71 tree_builder.file(repo_path, "").executable(executable);
72 tree_builder.write_merged_tree()
73 }
74
75 /// Build two commits that write the executable bit of a file as true/false.
76 #[track_caller]
77 fn prepare_exec_commits(ws: &TestWorkspace, repo_path: &RepoPath) -> (Commit, Commit) {
78 let store = ws.repo.store();
79 let tree_exec = create_tree_executable(store, repo_path, true);
80 let tree_no_exec = create_tree_executable(store, repo_path, false);
81
82 let commit_with_id = |id| testutils::commit_with_tree(ws.repo.store(), id);
83 let commit_exec = commit_with_id(tree_exec);
84 let commit_no_exec = commit_with_id(tree_no_exec);
85 assert_ne!(commit_exec, commit_no_exec);
86
87 (commit_exec, commit_no_exec)
88 }
89
90 /// Test that checking out a tree writes the correct executable bit to the
91 /// filesystem.
92 #[test]
93 fn test_exec_bit_checkout() -> TestResult {
94 let mut ws = TestWorkspace::init();
95 let path = &ws.workspace.workspace_root().join("file");
96 let repo_path = repo_path("file");
97
98 let (exec, no_exec) = prepare_exec_commits(&ws, repo_path);
99 let mut checkout_exec_commit = |executable| {
100 let commit = if executable { &exec } else { &no_exec };
101 let op_id = ws.repo.op_id().clone();
102 ws.workspace
103 .check_out(op_id, None, commit)
104 .block_on()
105 .unwrap();
106 };
107
108 // Checkout commits and ensure the filesystem is updated correctly.
109 assert!(!fs::exists(path)?);
110 for exec in [true, false, true] {
111 checkout_exec_commit(exec);
112 assert_file_executable(path, exec);
113 }
114 Ok(())
115 }
116
117 /// Test that snapshotting files stores the correct executable bit in the tree.
118 #[test]
119 fn test_exec_bit_snapshot() -> TestResult {
120 let mut ws = TestWorkspace::init();
121 let path = &ws.workspace.workspace_root().join("file");
122 let repo_path = repo_path("file");
123
124 // Snapshot, then assert the tree has the expected executable bit.
125 let mut snapshot_assert_exec_bit = |expected| {
126 let merged_tree_val = ws
127 .snapshot()
128 .unwrap()
129 .path_value(repo_path)
130 .block_on()
131 .unwrap();
132 let tree_val = merged_tree_val.into_resolved().unwrap().unwrap();
133 assert_tree_executable(tree_val, expected);
134 };
135
136 // Snapshot tree values when the file is/isn't executable.
137 fs::write(path, "initial content")?;
138 snapshot_assert_exec_bit(false);
139
140 fs::write(path, "first change")?;
141 snapshot_assert_exec_bit(false);
142
143 set_file_executable(path, true);
144 snapshot_assert_exec_bit(true);
145
146 fs::write(path, "second change")?;
147 snapshot_assert_exec_bit(true);
148
149 // Back to the same contents as before, but different exec bit.
150 fs::write(path, "first change")?;
151 set_file_executable(path, false);
152 snapshot_assert_exec_bit(false);
153
154 set_file_executable(path, true);
155 snapshot_assert_exec_bit(true);
156 Ok(())
157 }