2023-09-01 16:08:43 +00:00
|
|
|
// Copyright 2023 The Jujutsu Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use crate::common::TestEnvironment;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_snapshot_large_file() {
|
|
|
|
let test_env = TestEnvironment::default();
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
|
2023-09-01 16:08:43 +00:00
|
|
|
let repo_path = test_env.env_root().join("repo");
|
|
|
|
|
2024-04-04 05:38:53 +00:00
|
|
|
// test a small file using raw-integer-literal syntax, which is interpreted
|
|
|
|
// in bytes
|
cli: allow `snapshot.max-new-file-size` to be a raw u64
Previously, this command would work:
jj --config-toml='snapshot.max-new-file-size="1"' st
And is equivalent to this:
jj --config-toml='snapshot.max-new-file-size="1B"' st
But this would not work, despite looking like it should:
jj --config-toml='snapshot.max-new-file-size=1' st
This is extremely confusing for users.
This config value is deserialized via serde; and while the `HumanByteSize`
struct allegedly implemented Serde's `visit_u64` method, it was not called by
the deserialize visitor. Strangely, adding an `visit_i64` method *did* work, but
then requires handling of overflow, etc. This is likely because TOML integers
are naturally specified in `i64`.
Instead, just don't bother with any of that; implement a `TryFrom<String>`
instance for `HumanByteSize` that uses `u64::from_str` to try parsing the string
immediately; *then* fall back to `parse_human_byte_size` if that doesn't work.
This not only fixes the behavior but, IMO, is much simpler to reason about; we
get our `Deserialize` instance for free from the `TryFrom` instance.
Finally, this adjusts the test for `max-new-file-size` to now use a raw integer
literal, to ensure it doesn't regress. (There are already in-crate tests for
parsing the human readable strings.)
Signed-off-by: Austin Seipp <aseipp@pobox.com>
Change-Id: I8dafa2358d039ad1c07e9a512c1d10fed5845738
2024-03-28 20:45:31 +00:00
|
|
|
test_env.add_config(r#"snapshot.max-new-file-size = 10"#);
|
2023-09-01 16:08:43 +00:00
|
|
|
std::fs::write(repo_path.join("large"), "a lot of text").unwrap();
|
|
|
|
let stderr = test_env.jj_cmd_failure(&repo_path, &["files"]);
|
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
2024-01-31 11:45:03 +00:00
|
|
|
Error: Failed to snapshot the working copy
|
2024-03-28 20:45:31 +00:00
|
|
|
The file '$TEST_ENV/repo/large' is too large to be snapshotted: it is 3 bytes too large; the maximum size allowed is 10 bytes (10.0B).
|
|
|
|
Hint: This is to prevent large files from being added on accident. You can fix this error by:
|
|
|
|
- Adding the file to `.gitignore`
|
|
|
|
- Run `jj config set --repo snapshot.max-new-file-size 13`
|
|
|
|
This will increase the maximum file size allowed for new files, in this repository only.
|
|
|
|
- Run `jj --config-toml 'snapshot.max-new-file-size=13' st`
|
|
|
|
This will increase the maximum file size allowed for new files, for this command only.
|
2023-09-01 16:08:43 +00:00
|
|
|
"###);
|
2024-04-04 05:38:53 +00:00
|
|
|
|
|
|
|
// test with a larger file using 'KB' human-readable syntax
|
|
|
|
test_env.add_config(r#"snapshot.max-new-file-size = "10KB""#);
|
|
|
|
let big_string = vec![0; 1024 * 11];
|
|
|
|
std::fs::write(repo_path.join("large"), big_string).unwrap();
|
|
|
|
let stderr = test_env.jj_cmd_failure(&repo_path, &["files"]);
|
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
|
|
|
Error: Failed to snapshot the working copy
|
|
|
|
The file '$TEST_ENV/repo/large' is too large to be snapshotted: it is 1024 bytes too large; the maximum size allowed is 10240 bytes (10.0KiB).
|
|
|
|
Hint: This is to prevent large files from being added on accident. You can fix this error by:
|
|
|
|
- Adding the file to `.gitignore`
|
|
|
|
- Run `jj config set --repo snapshot.max-new-file-size 11264`
|
|
|
|
This will increase the maximum file size allowed for new files, in this repository only.
|
|
|
|
- Run `jj --config-toml 'snapshot.max-new-file-size=11264' st`
|
|
|
|
This will increase the maximum file size allowed for new files, for this command only.
|
|
|
|
"###);
|
2023-09-01 16:08:43 +00:00
|
|
|
}
|