| 1 |
// Copyright 2022 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 |
use std::io::Write as _; |
| 16 |
|
| 17 |
use testutils::TestResult; |
| 18 |
|
| 19 |
use crate::common::TestEnvironment; |
| 20 |
|
| 21 |
#[test] |
| 22 |
fn test_sparse_manage_patterns() { |
| 23 |
let mut test_env = TestEnvironment::default(); |
| 24 |
let edit_script = test_env.set_up_fake_editor(); |
| 25 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 26 |
let work_dir = test_env.work_dir("repo"); |
| 27 |
|
| 28 |
// Write some files to the working copy |
| 29 |
work_dir.write_file("file1", "contents"); |
| 30 |
work_dir.write_file("file2", "contents"); |
| 31 |
work_dir.write_file("file3", "contents"); |
| 32 |
|
| 33 |
// By default, all files are tracked |
| 34 |
let output = work_dir.run_jj(["sparse", "list"]); |
| 35 |
insta::assert_snapshot!(output, @" |
| 36 |
. |
| 37 |
[EOF] |
| 38 |
"); |
| 39 |
|
| 40 |
// Can stop tracking all files |
| 41 |
let output = work_dir.run_jj(["sparse", "set", "--remove", "."]); |
| 42 |
insta::assert_snapshot!(output, @" |
| 43 |
------- stderr ------- |
| 44 |
Added 0 files, modified 0 files, removed 3 files |
| 45 |
[EOF] |
| 46 |
"); |
| 47 |
// The list is now empty |
| 48 |
let output = work_dir.run_jj(["sparse", "list"]); |
| 49 |
insta::assert_snapshot!(output, @""); |
| 50 |
// They're removed from the working copy |
| 51 |
assert!(!work_dir.root().join("file1").exists()); |
| 52 |
assert!(!work_dir.root().join("file2").exists()); |
| 53 |
assert!(!work_dir.root().join("file3").exists()); |
| 54 |
// But they're still in the commit |
| 55 |
let output = work_dir.run_jj(["file", "list"]); |
| 56 |
insta::assert_snapshot!(output, @" |
| 57 |
file1 |
| 58 |
file2 |
| 59 |
file3 |
| 60 |
[EOF] |
| 61 |
"); |
| 62 |
|
| 63 |
// Run commands in sub directory to ensure that patterns are parsed as |
| 64 |
// workspace-relative paths, not cwd-relative ones. |
| 65 |
let sub_dir = work_dir.create_dir("sub"); |
| 66 |
|
| 67 |
// Not a workspace-relative path |
| 68 |
let output = sub_dir.run_jj(["sparse", "set", "--add=../file2"]); |
| 69 |
insta::assert_snapshot!(output, @r#" |
| 70 |
------- stderr ------- |
| 71 |
error: invalid value '../file2' for '--add <ADD>': Invalid component ".." in repo-relative path "../file2" |
| 72 |
|
| 73 |
For more information, try '--help'. |
| 74 |
[EOF] |
| 75 |
[exit status: 2] |
| 76 |
"#); |
| 77 |
|
| 78 |
// Can `--add` a few files |
| 79 |
let output = sub_dir.run_jj(["sparse", "set", "--add", "file2", "--add", "file3"]); |
| 80 |
insta::assert_snapshot!(output, @" |
| 81 |
------- stderr ------- |
| 82 |
Added 2 files, modified 0 files, removed 0 files |
| 83 |
[EOF] |
| 84 |
"); |
| 85 |
let output = sub_dir.run_jj(["sparse", "list"]); |
| 86 |
insta::assert_snapshot!(output, @" |
| 87 |
file2 |
| 88 |
file3 |
| 89 |
[EOF] |
| 90 |
"); |
| 91 |
assert!(!work_dir.root().join("file1").exists()); |
| 92 |
assert!(work_dir.root().join("file2").exists()); |
| 93 |
assert!(work_dir.root().join("file3").exists()); |
| 94 |
|
| 95 |
// Can combine `--add` and `--remove` |
| 96 |
let output = sub_dir.run_jj([ |
| 97 |
"sparse", "set", "--add", "file1", "--remove", "file2", "--remove", "file3", |
| 98 |
]); |
| 99 |
insta::assert_snapshot!(output, @" |
| 100 |
------- stderr ------- |
| 101 |
Added 1 files, modified 0 files, removed 2 files |
| 102 |
[EOF] |
| 103 |
"); |
| 104 |
let output = sub_dir.run_jj(["sparse", "list"]); |
| 105 |
insta::assert_snapshot!(output, @" |
| 106 |
file1 |
| 107 |
[EOF] |
| 108 |
"); |
| 109 |
assert!(work_dir.root().join("file1").exists()); |
| 110 |
assert!(!work_dir.root().join("file2").exists()); |
| 111 |
assert!(!work_dir.root().join("file3").exists()); |
| 112 |
|
| 113 |
// Can use `--clear` and `--add` |
| 114 |
let output = sub_dir.run_jj(["sparse", "set", "--clear", "--add", "file2"]); |
| 115 |
insta::assert_snapshot!(output, @" |
| 116 |
------- stderr ------- |
| 117 |
Added 1 files, modified 0 files, removed 1 files |
| 118 |
[EOF] |
| 119 |
"); |
| 120 |
let output = sub_dir.run_jj(["sparse", "list"]); |
| 121 |
insta::assert_snapshot!(output, @" |
| 122 |
file2 |
| 123 |
[EOF] |
| 124 |
"); |
| 125 |
assert!(!work_dir.root().join("file1").exists()); |
| 126 |
assert!(work_dir.root().join("file2").exists()); |
| 127 |
assert!(!work_dir.root().join("file3").exists()); |
| 128 |
|
| 129 |
// Can reset back to all files |
| 130 |
let output = sub_dir.run_jj(["sparse", "reset"]); |
| 131 |
insta::assert_snapshot!(output, @" |
| 132 |
------- stderr ------- |
| 133 |
Added 2 files, modified 0 files, removed 0 files |
| 134 |
[EOF] |
| 135 |
"); |
| 136 |
let output = sub_dir.run_jj(["sparse", "list"]); |
| 137 |
insta::assert_snapshot!(output, @" |
| 138 |
. |
| 139 |
[EOF] |
| 140 |
"); |
| 141 |
assert!(work_dir.root().join("file1").exists()); |
| 142 |
assert!(work_dir.root().join("file2").exists()); |
| 143 |
assert!(work_dir.root().join("file3").exists()); |
| 144 |
|
| 145 |
// Can edit with editor |
| 146 |
let edit_patterns = |patterns: &[&str]| { |
| 147 |
let mut file = std::fs::File::create(&edit_script).unwrap(); |
| 148 |
file.write_all(b"dump patterns0\0write\n").unwrap(); |
| 149 |
for pattern in patterns { |
| 150 |
file.write_all(pattern.as_bytes()).unwrap(); |
| 151 |
file.write_all(b"\n").unwrap(); |
| 152 |
} |
| 153 |
}; |
| 154 |
let read_patterns = || std::fs::read_to_string(test_env.env_root().join("patterns0")).unwrap(); |
| 155 |
|
| 156 |
edit_patterns(&["file1"]); |
| 157 |
let output = sub_dir.run_jj(["sparse", "edit"]); |
| 158 |
insta::assert_snapshot!(output, @" |
| 159 |
------- stderr ------- |
| 160 |
Added 0 files, modified 0 files, removed 2 files |
| 161 |
[EOF] |
| 162 |
"); |
| 163 |
insta::assert_snapshot!(read_patterns(), @"."); |
| 164 |
let output = sub_dir.run_jj(["sparse", "list"]); |
| 165 |
insta::assert_snapshot!(output, @" |
| 166 |
file1 |
| 167 |
[EOF] |
| 168 |
"); |
| 169 |
|
| 170 |
// Can edit with multiple files |
| 171 |
edit_patterns(&["file3", "file2", "file3"]); |
| 172 |
let output = sub_dir.run_jj(["sparse", "edit"]); |
| 173 |
insta::assert_snapshot!(output, @" |
| 174 |
------- stderr ------- |
| 175 |
Added 2 files, modified 0 files, removed 1 files |
| 176 |
[EOF] |
| 177 |
"); |
| 178 |
insta::assert_snapshot!(read_patterns(), @"file1"); |
| 179 |
let output = sub_dir.run_jj(["sparse", "list"]); |
| 180 |
insta::assert_snapshot!(output, @" |
| 181 |
file2 |
| 182 |
file3 |
| 183 |
[EOF] |
| 184 |
"); |
| 185 |
|
| 186 |
// Invalid paths are rejected |
| 187 |
edit_patterns(&["./file1"]); |
| 188 |
let output = sub_dir.run_jj(["sparse", "edit"]); |
| 189 |
insta::assert_snapshot!(output, @r#" |
| 190 |
------- stderr ------- |
| 191 |
Error: Failed to parse sparse pattern: ./file1 |
| 192 |
Caused by: Invalid component "." in repo-relative path "./file1" |
| 193 |
[EOF] |
| 194 |
[exit status: 1] |
| 195 |
"#); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn test_sparse_editor_avoids_unc() -> TestResult { |
| 200 |
use std::path::PathBuf; |
| 201 |
|
| 202 |
let mut test_env = TestEnvironment::default(); |
| 203 |
let edit_script = test_env.set_up_fake_editor(); |
| 204 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 205 |
let work_dir = test_env.work_dir("repo"); |
| 206 |
|
| 207 |
std::fs::write(edit_script, "dump-path path")?; |
| 208 |
work_dir.run_jj(["sparse", "edit"]).success(); |
| 209 |
|
| 210 |
let edited_path = PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path"))?); |
| 211 |
// While `assert!(!edited_path.starts_with("//?/"))` could work here in most |
| 212 |
// cases, it fails when it is not safe to strip the prefix, such as paths |
| 213 |
// over 260 chars. |
| 214 |
assert_eq!(edited_path, dunce::simplified(&edited_path)); |
| 215 |
Ok(()) |
| 216 |
} |