Skip to content

Commit

Permalink
add tests for fmt config options. (#230)
Browse files Browse the repository at this point in the history
This commit adds tests for the configurable formatter. Each configuration option
is tested individually, as they each should be independent of each other. If we
have a need to test multiple configuration options simultaneously in the future
this setup should be able to do it easily.

While here I also found a bug in how tab stops were treated by the formatter
when removing trailing whitespaces. After indentation was performed the step to
remove trailing whitespace was not properly checking for the inserted tab
characters.

There was also another bug with handling the emptyline before the section header
which I fixed.

Also, fix a bug in the docs where the `meta.align_values` and
`meta.align_patterns` options were incorrectly set. They changed to true in a
previous commit but I did not update the docs then.
  • Loading branch information
wxsBSD authored and plusvic committed Oct 27, 2024
1 parent ea10015 commit a2f18d6
Show file tree
Hide file tree
Showing 83 changed files with 184 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/YARA-X Config Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ The `yr` command looks in `${HOME}/.yara-x.toml` when starting up. If that file
does not exist the default values are used.

An example `.yara-x.toml` file is below, with comments that explain each option.
The values for each option are the default values that are used if omit the
option.
The values for each option are the default values that are used if the option
is omitted.

This is the definitive list of supported configuration options, and will be
updated as more are added.
Expand Down Expand Up @@ -139,8 +139,8 @@ rule.empty_line_after_section_header = false
#
# Note that alignment is done with spaces, regardless of rule.indent_spaces
# setting.
meta.align_values = false
meta.align_values = true

# Same as meta.align_values but applies to patterns.
patterns.align_values = false
patterns.align_values = true
```
1 change: 0 additions & 1 deletion fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ impl Formatter {
| Keyword(b"strings")
| Keyword(b"condition")
) && ctx.token(-1).is_not(*NEWLINE)
&& ctx.token(-2).is_not(*NEWLINE)
},
processor::actions::emptyline,
)
Expand Down
12 changes: 12 additions & 0 deletions fmt/src/testdata/config_tests/align_metadata_false.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rule test {
meta:
a = "b"
long = "foo"

strings:
$a = "b"
$axs = { 06 11 }

condition:
$a
}
12 changes: 12 additions & 0 deletions fmt/src/testdata/config_tests/align_patterns_false.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rule test {
meta:
a = "b"
long = "foo"

strings:
$a = "b"
$axs = { 06 11 }

condition:
$a
}
1 change: 1 addition & 0 deletions fmt/src/testdata/config_tests/align_rule.unformatted
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rule test{meta:a="b"long="foo"strings:$a="b"$axs={0611}condition:$a}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rule test {
meta:

a = "b"

strings:

$a = "b"

condition:

$a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rule test {
meta:
a = "b"
strings:
$a = "b"
condition:
$a
}
2 changes: 2 additions & 0 deletions fmt/src/testdata/config_tests/generic_rule.unformatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

rule test{meta:a="b"strings:$a="b"condition:$a}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rule test {
meta:
a = "b"

strings:
$a = "b"

condition:
$a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rule test {
meta:
a = "b"

strings:
$a = "b"

condition:
$a
}
10 changes: 10 additions & 0 deletions fmt/src/testdata/config_tests/indent_spaces_one.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rule test {
meta:
a = "b"

strings:
$a = "b"

condition:
$a
}
10 changes: 10 additions & 0 deletions fmt/src/testdata/config_tests/indent_spaces_zero.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rule test {
meta:
a = "b"

strings:
$a = "b"

condition:
$a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
rule test
{
meta:
a = "b"

strings:
$a = "b"

condition:
$a
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
84 changes: 79 additions & 5 deletions fmt/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::{fs, str};

use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -39,11 +40,12 @@ fn spacer() {

#[test]
fn format() {
let files: Vec<_> = globwalk::glob("src/testdata/*.unformatted")
.unwrap()
.flatten()
.map(|entry| entry.into_path())
.collect();
let files: Vec<_> =
globwalk::glob("src/testdata/default_tests/*.unformatted")
.unwrap()
.flatten()
.map(|entry| entry.into_path())
.collect();

files.into_par_iter().for_each(|path| {
let mut mint = goldenfile::Mint::new(".");
Expand All @@ -61,3 +63,75 @@ fn format() {
}
});
}

#[test]
fn format_config_options() {
// Tuples for tests. First item is the formatter config to use. Second item
// is the unformatted test file. Third item is expected formatted file.
let tests = vec![
(
Formatter::new().indent_section_headers(false),
"generic_rule.unformatted",
"indent_section_headers_false.formatted",
),
(
Formatter::new().indent_section_contents(false),
"generic_rule.unformatted",
"indent_section_contents_false.formatted",
),
(
Formatter::new().indent_spaces(0),
"generic_rule.unformatted",
"indent_spaces_zero.formatted",
),
(
Formatter::new().indent_spaces(1),
"generic_rule.unformatted",
"indent_spaces_one.formatted",
),
(
Formatter::new().newline_before_curly_brace(true),
"generic_rule.unformatted",
"newline_before_curly_brace_true.formatted",
),
(
Formatter::new().empty_line_before_section_header(false),
"generic_rule.unformatted",
"empty_line_before_section_header_false.formatted",
),
(
Formatter::new().empty_line_after_section_header(true),
"generic_rule.unformatted",
"empty_line_after_section_header_true.formatted",
),
(
Formatter::new().align_metadata(false),
"align_rule.unformatted",
"align_metadata_false.formatted",
),
(
Formatter::new().align_patterns(false),
"align_rule.unformatted",
"align_patterns_false.formatted",
),
];

let base = PathBuf::from("src/testdata/config_tests/");
for (formatter, unformatted, formatted) in tests {
let input = fs::read_to_string(base.join(unformatted))
.expect("error reading unformatted file");
let expected = fs::read_to_string(base.join(formatted))
.expect("error reading formatted file");
let mut output = Vec::new();

formatter
.format(input.as_bytes(), &mut output)
.expect("format failed");
assert_eq!(
str::from_utf8(&output).unwrap(),
expected,
"Formatted file: {}",
formatted
);
}
}
4 changes: 2 additions & 2 deletions fmt/src/trailing_spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ where
for next in self.input.by_ref() {
match next {
// Keep pushing tokens into the buffer while they are
// whitespaces
Token::Whitespace => {
// whitespaces or tabs
Token::Whitespace | Token::Tab => {
self.output_buffer.push_back(next);
}
// If we find a newline, discard all whitespaces previously
Expand Down

0 comments on commit a2f18d6

Please sign in to comment.