| 1 |
// Copyright 2026 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 indoc::indoc; |
| 16 |
|
| 17 |
use crate::common::TestEnvironment; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_alias() { |
| 21 |
let test_env = TestEnvironment::default(); |
| 22 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 23 |
let work_dir = test_env.work_dir("repo"); |
| 24 |
work_dir.write_file("file1", ""); |
| 25 |
work_dir.write_file("file2", ""); |
| 26 |
|
| 27 |
test_env.add_config(indoc! {" |
| 28 |
[fileset-aliases] |
| 29 |
'star' = '*' |
| 30 |
'syntax-error' = 'whatever &' |
| 31 |
'recurse' = 'recurse1' |
| 32 |
'recurse1' = 'recurse2()' |
| 33 |
'recurse2()' = 'recurse' |
| 34 |
'identity(x)' = 'x' |
| 35 |
'not:x' = '~x' |
| 36 |
"}); |
| 37 |
let query = |arg: &str| work_dir.run_jj(["file", "list", arg]); |
| 38 |
|
| 39 |
insta::assert_snapshot!(query("star"), @" |
| 40 |
file1 |
| 41 |
file2 |
| 42 |
[EOF] |
| 43 |
"); |
| 44 |
|
| 45 |
insta::assert_snapshot!(query("identity(file1)"), @" |
| 46 |
file1 |
| 47 |
[EOF] |
| 48 |
"); |
| 49 |
|
| 50 |
insta::assert_snapshot!(query("not:file1"), @" |
| 51 |
file2 |
| 52 |
[EOF] |
| 53 |
"); |
| 54 |
|
| 55 |
insta::assert_snapshot!(query("file1 | syntax-error"), @" |
| 56 |
------- stderr ------- |
| 57 |
Error: Failed to parse fileset: In alias `syntax-error` |
| 58 |
Caused by: |
| 59 |
1: --> 1:9 |
| 60 |
| |
| 61 |
1 | file1 | syntax-error |
| 62 |
| ^----------^ |
| 63 |
| |
| 64 |
= In alias `syntax-error` |
| 65 |
2: --> 1:11 |
| 66 |
| |
| 67 |
1 | whatever & |
| 68 |
| ^--- |
| 69 |
| |
| 70 |
= expected `~` or <primary> |
| 71 |
[EOF] |
| 72 |
[exit status: 1] |
| 73 |
"); |
| 74 |
|
| 75 |
insta::assert_snapshot!(query("identity(unknown:pat)"), @" |
| 76 |
------- stderr ------- |
| 77 |
Error: Failed to parse fileset: In alias `identity(x)` |
| 78 |
Caused by: |
| 79 |
1: --> 1:1 |
| 80 |
| |
| 81 |
1 | identity(unknown:pat) |
| 82 |
| ^-------------------^ |
| 83 |
| |
| 84 |
= In alias `identity(x)` |
| 85 |
2: --> 1:1 |
| 86 |
| |
| 87 |
1 | x |
| 88 |
| ^ |
| 89 |
| |
| 90 |
= In function parameter `x` |
| 91 |
3: --> 1:10 |
| 92 |
| |
| 93 |
1 | identity(unknown:pat) |
| 94 |
| ^---------^ |
| 95 |
| |
| 96 |
= Invalid file pattern |
| 97 |
4: Invalid file pattern kind `unknown:` |
| 98 |
[EOF] |
| 99 |
[exit status: 1] |
| 100 |
"); |
| 101 |
|
| 102 |
insta::assert_snapshot!(query("file1 & recurse"), @" |
| 103 |
------- stderr ------- |
| 104 |
Error: Failed to parse fileset: In alias `recurse` |
| 105 |
Caused by: |
| 106 |
1: --> 1:9 |
| 107 |
| |
| 108 |
1 | file1 & recurse |
| 109 |
| ^-----^ |
| 110 |
| |
| 111 |
= In alias `recurse` |
| 112 |
2: --> 1:1 |
| 113 |
| |
| 114 |
1 | recurse1 |
| 115 |
| ^------^ |
| 116 |
| |
| 117 |
= In alias `recurse1` |
| 118 |
3: --> 1:1 |
| 119 |
| |
| 120 |
1 | recurse2() |
| 121 |
| ^--------^ |
| 122 |
| |
| 123 |
= In alias `recurse2()` |
| 124 |
4: --> 1:1 |
| 125 |
| |
| 126 |
1 | recurse |
| 127 |
| ^-----^ |
| 128 |
| |
| 129 |
= Alias `recurse` expanded recursively |
| 130 |
[EOF] |
| 131 |
[exit status: 1] |
| 132 |
"); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_alias_in_revset_or_template() { |
| 137 |
let test_env = TestEnvironment::default(); |
| 138 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 139 |
let work_dir = test_env.work_dir("repo"); |
| 140 |
work_dir.write_file("file1", ""); |
| 141 |
|
| 142 |
test_env.add_config(indoc! {" |
| 143 |
[fileset-aliases] |
| 144 |
'star' = '*' |
| 145 |
"}); |
| 146 |
|
| 147 |
let output = work_dir.run_jj(["log", "-rfiles(star)", "--summary"]); |
| 148 |
insta::assert_snapshot!(output, @" |
| 149 |
@ qpvuntsm [email protected] 2001-02-03 08:05:08 093c3c96 |
| 150 |
│ (no description set) |
| 151 |
~ A file1 |
| 152 |
[EOF] |
| 153 |
"); |
| 154 |
|
| 155 |
let output = work_dir.run_jj(["log", "-r@", "-Tself.diff('star').summary()"]); |
| 156 |
insta::assert_snapshot!(output, @" |
| 157 |
@ A file1 |
| 158 |
│ |
| 159 |
~ |
| 160 |
[EOF] |
| 161 |
"); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn test_alias_override() { |
| 166 |
let test_env = TestEnvironment::default(); |
| 167 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 168 |
let work_dir = test_env.work_dir("repo"); |
| 169 |
|
| 170 |
test_env.add_config(indoc! {" |
| 171 |
[fileset-aliases] |
| 172 |
'f(x)' = 'user' |
| 173 |
"}); |
| 174 |
|
| 175 |
// 'f(x)' should be overridden by --config 'f(a)'. If aliases were sorted |
| 176 |
// purely by name, 'f(a)' would come first. |
| 177 |
let output = work_dir.run_jj([ |
| 178 |
"file", |
| 179 |
"list", |
| 180 |
"f(_)", |
| 181 |
"--config=fileset-aliases.'f(a)'=arg", |
| 182 |
]); |
| 183 |
insta::assert_snapshot!(output, @" |
| 184 |
------- stderr ------- |
| 185 |
Warning: No matching entries for paths: arg |
| 186 |
[EOF] |
| 187 |
"); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn test_bad_alias_decl() { |
| 192 |
let test_env = TestEnvironment::default(); |
| 193 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 194 |
let work_dir = test_env.work_dir("repo"); |
| 195 |
work_dir.write_file("file1", ""); |
| 196 |
|
| 197 |
test_env.add_config(indoc! {r#" |
| 198 |
[fileset-aliases] |
| 199 |
'star' = '*' |
| 200 |
'"bad"' = 'root()' |
| 201 |
'badfn(a, a)' = 'root()' |
| 202 |
"#}); |
| 203 |
|
| 204 |
// Invalid declaration should be warned and ignored. |
| 205 |
let output = work_dir.run_jj(["file", "list", "star"]); |
| 206 |
insta::assert_snapshot!(output, @r#" |
| 207 |
file1 |
| 208 |
[EOF] |
| 209 |
------- stderr ------- |
| 210 |
Warning: Failed to load `fileset-aliases."bad"`: --> 1:1 |
| 211 |
| |
| 212 |
1 | "bad" |
| 213 |
| ^--- |
| 214 |
| |
| 215 |
= expected <strict_identifier> or <function_name> |
| 216 |
Warning: Failed to load `fileset-aliases.badfn(a, a)`: --> 1:7 |
| 217 |
| |
| 218 |
1 | badfn(a, a) |
| 219 |
| ^--^ |
| 220 |
| |
| 221 |
= Redefinition of function parameter |
| 222 |
[EOF] |
| 223 |
"#); |
| 224 |
} |