cargo-sort 2.1.4

Check if tables and items in a .toml file are lexically sorted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::{fmt::Display, fs::read_to_string, io::Write, path::PathBuf};

use fmt::Config;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use toml_edit::{DocumentMut, Item};

mod fmt;
mod sort;
#[cfg(test)]
mod test_utils;

const EXTRA_HELP: &str = "\
    NOTE: in check mode, only unsorted dependencies cause failure; \
          formatting differences are reported as warnings unless --check-format is used";

type IoResult<T> = Result<T, Box<dyn std::error::Error>>;

/// Ensure Cargo.toml dependency tables are sorted.
#[derive(clap::Parser, Debug)]
#[command(author, version, bin_name = "cargo sort", after_help = EXTRA_HELP)]
pub struct Cli {
    /// sets cwd, must contain a Cargo.toml file
    #[arg(value_name = "CWD")]
    pub cwd: Vec<String>,

    /// Returns non-zero exit code if Cargo.toml is unsorted, overrides default behavior
    #[arg(short, long)]
    pub check: bool,

    /// Prints Cargo.toml, lexically sorted, to stdout
    #[arg(short, long, conflicts_with = "check")]
    pub print: bool,

    /// Skips formatting after sorting
    #[arg(short = 'n', long)]
    pub no_format: bool,

    /// Also returns non-zero exit code if formatting changes
    #[arg(long, requires = "check")]
    pub check_format: bool,

    /// Checks every crate in a workspace
    #[arg(short, long)]
    pub workspace: bool,

    /// Keep blank lines when sorting groups of key value pairs
    #[arg(short, long)]
    pub grouped: bool,

    /// List the order tables should be written out
    /// (--order package,dependencies,features)
    #[arg(short, long, value_delimiter = ',')]
    pub order: Vec<String>,

    /// Path to a custom config file (tomlfmt.toml)
    #[arg(long, value_name = "PATH")]
    pub config: Option<PathBuf>,
}

fn write_red<S: Display>(highlight: &str, msg: S) -> IoResult<()> {
    let mut stderr = StandardStream::stderr(ColorChoice::Auto);
    stderr.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;
    write!(stderr, "{highlight}")?;
    stderr.reset()?;
    writeln!(stderr, "{msg}").map_err(Into::into)
}

fn write_green<S: Display>(highlight: &str, msg: S) -> IoResult<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Auto);
    stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
    write!(stdout, "{highlight}")?;
    stdout.reset()?;
    writeln!(stdout, "{msg}").map_err(Into::into)
}

fn write_yellow<S: Display>(highlight: &str, msg: S) -> IoResult<()> {
    let mut stderr = StandardStream::stderr(ColorChoice::Auto);
    stderr.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)))?;
    write!(stderr, "{highlight}")?;
    stderr.reset()?;
    writeln!(stderr, "{msg}").map_err(Into::into)
}

struct ProcessedToml {
    is_sorted: bool,
    is_formatted: bool,
    final_output: String,
}

fn process_toml(
    toml_raw: &str,
    grouped: bool,
    no_format: bool,
    check_format: bool,
    config: &Config,
) -> ProcessedToml {
    let mut sorted =
        sort::sort_toml(toml_raw, sort::MATCHER, grouped, &config.table_order);
    let sorted_only = sorted.to_string();
    // sort_toml/toml_edit always outputs LF, but input may have CRLF. Normalize
    // the raw input for comparison's sake only (formatting checking/fix is
    // unaffected).
    let toml_normalized = toml_raw.replace("\r\n", "\n");
    let is_sorted = toml_normalized == sorted_only;

    let (final_output, is_formatted) = if !no_format || check_format {
        fmt::fmt_toml(&mut sorted, config);
        let formatted = sorted.to_string();
        let is_fmt = sorted_only == formatted;
        (formatted, is_fmt)
    } else {
        (sorted_only, true)
    };

    let final_output =
        if config.crlf.unwrap_or(fmt::DEF_CRLF) && !final_output.contains("\r\n") {
            final_output.replace('\n', "\r\n")
        } else {
            final_output
        };

    ProcessedToml { is_sorted, is_formatted, final_output }
}

fn check_toml(path: &str, cli: &Cli, config: &Config) -> IoResult<bool> {
    let mut path = PathBuf::from(path);
    if path.is_dir() {
        path.push("Cargo.toml");
    }

    let krate = path.components().nth_back(1).ok_or("No crate folder found")?.as_os_str();

    write_green("Checking ", format!("{}...", krate.to_string_lossy()))?;

    let toml_raw = read_to_string(&path)
        .map_err(|_| format!("No file found at: {}", path.display()))?;

    let crlf = toml_raw.contains("\r\n");

    let mut config = config.clone();
    if config.crlf.is_none() {
        config.crlf = Some(crlf);
    }

    let result =
        process_toml(&toml_raw, cli.grouped, cli.no_format, cli.check_format, &config);

    if cli.print {
        print!("{}", result.final_output);
        return Ok(true);
    }

    if cli.check {
        if !result.is_sorted {
            write_red(
                "error: ",
                format!("Dependencies for {} are not sorted", krate.to_string_lossy()),
            )?;
        }

        if !result.is_formatted {
            if cli.check_format {
                write_red(
                    "error: ",
                    format!(
                        "Cargo.toml for {} is not formatted",
                        krate.to_string_lossy()
                    ),
                )?;
            } else {
                write_yellow(
                    "warning: ",
                    format!(
                        "Cargo.toml for {} is not formatted",
                        krate.to_string_lossy()
                    ),
                )?;
            }
        }

        return Ok(result.is_sorted && (!cli.check_format || result.is_formatted));
    }

    let has_changes = toml_raw != result.final_output;
    if has_changes {
        std::fs::write(&path, &result.final_output)?;
        write_green(
            "Finished: ",
            format!("Cargo.toml for {:?} has been rewritten", krate.to_string_lossy()),
        )?;
    } else {
        write_green(
            "Finished: ",
            format!(
                "Cargo.toml for {} is sorted already, no changes made",
                krate.to_string_lossy()
            ),
        )?;
    }

    Ok(true)
}

fn _main() -> IoResult<()> {
    let mut args: Vec<String> = std::env::args().collect();
    // remove "sort" when invoked `cargo sort` sort is the first arg
    // https://github.com/rust-lang/cargo/issues/7653
    if args.len() > 1 && args[1] == "sort" {
        args.remove(1);
    }
    let cli = <Cli as clap::Parser>::parse_from(args);

    let cwd = std::env::current_dir()
        .map_err(|e| format!("no current directory found: {e}"))?;
    let dir = cwd.to_string_lossy();

    let mut filtered_matches: Vec<String> = cli.cwd.clone();
    let is_posible_workspace = filtered_matches.is_empty() || filtered_matches.len() == 1;
    if filtered_matches.is_empty() {
        filtered_matches.push(dir.to_string());
    }

    if cli.workspace && is_posible_workspace {
        let dir = filtered_matches[0].to_string();
        let mut path = PathBuf::from(&dir);
        if path.is_dir() {
            path.push("Cargo.toml");
        }

        let raw_toml = read_to_string(&path)
            .map_err(|_| format!("no file found at: {}", path.display()))?;

        let toml = raw_toml.parse::<DocumentMut>()?;
        let workspace = toml.get("workspace");
        if let Some(Item::Table(ws)) = workspace {
            // The workspace excludes, used to filter members by
            let excludes: Vec<&str> =
                ws.get("exclude").map_or_else(Vec::new, array_string_members);
            for member in ws.get("members").map_or_else(Vec::new, array_string_members) {
                // TODO: a better test wether to glob?
                if member.contains('*') || member.contains('?') {
                    'globs: for entry in glob::glob(&format!("{dir}/{member}"))
                        .unwrap_or_else(|e| {
                            write_red("error: ", format!("Glob failed: {e}")).unwrap();
                            std::process::exit(1);
                        })
                    {
                        let path = entry?;

                        // The `check_toml` function expects only folders that it appends
                        // `Cargo.toml` onto
                        if path.is_file() {
                            continue;
                        }

                        // Since the glob function gives us actual paths we need to only
                        // check if the relevant parts match so we can't just do
                        // `excludes.contains(..)`
                        let path_str = path.to_string_lossy();
                        for excl in &excludes {
                            if path_str.ends_with(excl) {
                                continue 'globs;
                            }
                        }

                        filtered_matches.push(path.display().to_string());
                    }
                } else {
                    filtered_matches.push(format!("{dir}/{member}"));
                }
            }
        }
    }

    let mut config = if let Some(config_path) = &cli.config {
        read_to_string(config_path)
            .map_err(|_| format!("config file not found: {}", config_path.display()))?
            .parse::<Config>()?
    } else {
        let mut config_path = cwd.clone();
        config_path.push("tomlfmt.toml");
        read_to_string(&config_path)
            .or_else(|_err| {
                config_path.pop();
                config_path.push(".tomlfmt.toml");
                read_to_string(&config_path)
            })
            .unwrap_or_default()
            .parse::<Config>()?
    };

    if !cli.order.is_empty() {
        config.table_order = cli.order.clone();
    }

    let mut flag = true;
    for sorted in filtered_matches.iter().map(|path| check_toml(path, &cli, &config)) {
        if !(sorted?) {
            flag = false;
        }
    }

    if !flag {
        return Err("Some Cargo.toml files are not sorted or formatted".into());
    }
    Ok(())
}

fn array_string_members(value: &Item) -> Vec<&str> {
    value.as_array().into_iter().flatten().filter_map(|s| s.as_str()).collect()
}

fn main() {
    _main().unwrap_or_else(|e| {
        write_red("error: ", e).unwrap();
        std::process::exit(1);
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cli_config_flag_parsing() {
        let args = vec!["cargo-sort", "--config", "custom.toml"];
        let cli = <Cli as clap::Parser>::try_parse_from(args).unwrap();
        assert_eq!(cli.config, Some(PathBuf::from("custom.toml")));
    }

    #[test]
    fn cli_config_flag_with_other_flags() {
        let args = vec!["cargo-sort", "--check", "--config", "/path/to/config.toml", "."];
        let cli = <Cli as clap::Parser>::try_parse_from(args).unwrap();
        assert!(cli.check);
        assert_eq!(cli.config, Some(PathBuf::from("/path/to/config.toml")));
        assert_eq!(cli.cwd, vec!["."]);
    }

    #[test]
    fn cli_without_config_flag() {
        let args = vec!["cargo-sort", "--check", "."];
        let cli = <Cli as clap::Parser>::try_parse_from(args).unwrap();
        assert_eq!(cli.config, None);
    }

    #[test]
    fn custom_config_loads_correctly() {
        let config_content = read_to_string("examp/custom_config.toml").unwrap();
        let config: Config = config_content.parse().unwrap();
        assert!(config.always_trailing_comma);
        assert!(!config.multiline_trailing_comma);
        assert!(!config.space_around_eq);
        assert!(config.compact_arrays);
        assert_eq!(config.indent_count, 2);
    }

    #[test]
    fn config_error_on_missing_file() {
        let result = read_to_string("nonexistent_config.toml");
        assert!(result.is_err());
    }

    #[test]
    fn check_unsorted() {
        let toml = "[dependencies]\nfoo = \"1\"\nbar = \"1\"\n";
        let config = Config::default();
        let result = process_toml(toml, false, false, false, &config);
        assert!(!result.is_sorted);
        assert!(result.is_formatted);
    }

    #[test]
    fn check_sorted_unformatted() {
        // Sorted deps, but missing space around '=' so formatting differs
        let toml = "[dependencies]\nbar=\"1\"\nfoo=\"1\"\n";
        let config = Config::default();
        let result = process_toml(toml, false, false, false, &config);
        assert!(result.is_sorted);
        assert!(!result.is_formatted);
    }

    #[test]
    fn sorted_unformatted_no_check_format() {
        let toml = "[dependencies]\nbar=\"1\"\nfoo=\"1\"\n";
        let config = Config::default();
        let result = process_toml(toml, false, false, true, &config);
        assert!(result.is_sorted);
        assert!(!result.is_formatted);
    }

    #[test]
    fn sorted_with_crlf_detected_as_sorted() {
        let toml = "[dependencies]\r\nbar = \"1\"\r\nfoo = \"1\"\r\n";
        let config = Config::default();
        let result = process_toml(toml, false, false, false, &config);
        assert!(
            result.is_sorted,
            "CRLF file with sorted deps should be detected as sorted"
        );
    }
}

// #[test]
// fn fuzzy_fail() {
//     for file in std::fs::read_dir("out/default/crashes").unwrap() {
//         let path = file.unwrap().path();
//         println!("{}", path.display());
//         let s = read_to_string(&path).unwrap().replace("\r", "");
//         let mut toml = sort::sort_toml(&s, sort::MATCHER, false);
//         fmt::fmt_toml(&mut toml, &fmt::Config::default());
//         print!("{}", s);
//         s.parse::<DocumentMut>().unwrap();
//     }
// }