Skip to content

Commit fe36433

Browse files
authored
feat: allow overriding max file size limit (#616)
1 parent 2d88312 commit fe36433

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

crates/cli_bin/tests/apply.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,3 +3267,57 @@ fn apply_remote_pattern() -> Result<()> {
32673267

32683268
Ok(())
32693269
}
3270+
3271+
#[test]
3272+
fn large_file_fails() -> Result<()> {
3273+
let tempdir = tempfile::tempdir()?;
3274+
let large_file = tempdir.path().join("large.js");
3275+
3276+
// Create a large file with many console.log statements
3277+
// Each line is about 50 bytes, so 25,000 lines = ~1.25MB
3278+
let mut content = String::with_capacity(1_250_000);
3279+
for i in 0..25_000 {
3280+
content.push_str(&format!("console.log('This is log message number {i} which should make the file quite large');\n"));
3281+
}
3282+
content.push_str("console.error('This is an error message, at the end');");
3283+
fs_err::write(&large_file, content)?;
3284+
3285+
let mut apply_cmd = get_test_cmd()?;
3286+
apply_cmd.current_dir(tempdir.path());
3287+
apply_cmd
3288+
.arg("apply")
3289+
.arg("`console.error` => `console.warn`")
3290+
.arg("large.js");
3291+
3292+
let output = apply_cmd.output()?;
3293+
let stdout = String::from_utf8(output.stdout)?;
3294+
let stderr = String::from_utf8(output.stderr)?;
3295+
3296+
println!("stdout first time: {:?}", stdout);
3297+
println!("stderr first time: {:?}", stderr);
3298+
3299+
// Command should succeed but with a warning about file size
3300+
assert!(output.status.success());
3301+
assert!(stdout.contains("Processed 1 files and found 0 matches"));
3302+
3303+
// Verify that the file is unmodified
3304+
let content: String = fs_err::read_to_string(large_file.clone())?;
3305+
assert!(!content.contains("console.warn"));
3306+
3307+
println!("Successfully ran the command the first time");
3308+
3309+
// Now run the command again, but with GRIT_MAX_FILE_SIZE=0
3310+
let new_command = apply_cmd.env("GRIT_MAX_FILE_SIZE_BYTES", "0");
3311+
let output = new_command.output()?;
3312+
let stdout = String::from_utf8(output.stdout)?;
3313+
let stderr = String::from_utf8(output.stderr)?;
3314+
3315+
println!("stdout second time: {:?}", stdout);
3316+
println!("stderr second time: {:?}", stderr);
3317+
3318+
// Verify that the file is modified
3319+
let content: String = fs_err::read_to_string(large_file)?;
3320+
assert!(content.contains("console.warn"));
3321+
3322+
Ok(())
3323+
}

crates/core/src/limits.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
use crate::constants::MAX_FILE_SIZE;
12
use grit_util::{AnalysisLog, Position};
23
use marzano_util::rich_path::RichFile;
3-
4-
use crate::constants::MAX_FILE_SIZE;
4+
use std::env;
55

66
pub(crate) fn is_file_too_big(file: &RichFile) -> Option<AnalysisLog> {
7-
if file.path.len() > MAX_FILE_SIZE || file.content.len() > MAX_FILE_SIZE {
7+
let max_size = env::var("GRIT_MAX_FILE_SIZE_BYTES")
8+
.ok()
9+
.and_then(|s| s.parse::<usize>().ok())
10+
.unwrap_or(MAX_FILE_SIZE);
11+
12+
// Skip the check if max_size is 0
13+
if max_size == 0 {
14+
return None;
15+
}
16+
17+
if file.path.len() > max_size || file.content.len() > max_size {
818
Some(AnalysisLog {
919
// TODO: standardize levels
1020
level: Some(310),

0 commit comments

Comments
 (0)