Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

test: custom tests for formatter #2045

Merged
merged 12 commits into from
Feb 9, 2022
Merged

Conversation

ematipico
Copy link
Contributor

@ematipico ematipico commented Feb 3, 2022

Summary

Closes #2044

Updated the test suite of the formatter. Now, it's possible to create test cases with a particular set of options by creating a options.json file beside the test file we want to test.

All files inside a folder that contains options.json will be executed with the set of options written inside the file. Meaning that if you wanted to run only one particular file with some particular options, you would need to put the file inside a separate folder (the PR has an example).

I slightly changed the output of the snapshot, that's why there are lot of changes.

Test Plan

Updated the current snapshots and create a new case with new output and verified that it is written.

@ematipico ematipico force-pushed the test/custom-tests-formatter branch from b659af6 to 70cbfb3 Compare February 3, 2022 15:00
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 3, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 59945e4
Status:⚡️  Build in progress...

View logs

@ematipico ematipico force-pushed the test/custom-tests-formatter branch from 70cbfb3 to 066be64 Compare February 3, 2022 15:03
@github-actions
Copy link

github-actions bot commented Feb 3, 2022

Parser conformance results on ubuntu-latest

T262

Test result main count This PR count Difference
Total 45250 45250 0
Passed 44130 44130 0
Failed 1120 1120 0
Panics 0 0 0
Coverage 97.52% 97.52% 0.00%

TS

Test result main count This PR count Difference
Total 15976 15976 0
Passed 11213 11213 0
Failed 4762 4762 0
Panics 1 1 0
Coverage 70.19% 70.19% 0.00%

console.log(import.meta);
import.meta.field =
obj.aReallyLongVariableName.andAnotherReallyLongVariableName.andAnotherReallyLongVariableName.andAnotherReallyLongVariable;
import.meta.aReallyLongVariableName.andAnotherReallyLongVariableName.andAnotherReallyLongVariableName.andAnotherReallyLongVariable;
## Output 2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new output

@ematipico ematipico requested a review from a team February 3, 2022 15:33
@MichaReiser MichaReiser requested a review from leops February 4, 2022 13:51
Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I like the idea and code looks good to me. Let's wait for @xunilrj or @leops to chime in to see if the cfg approach is the right way to go

@ematipico ematipico requested a review from xunilrj February 4, 2022 13:58
crates/rome_formatter/tests/spec_test.rs Outdated Show resolved Hide resolved
crates/rome_formatter/tests/spec_test.rs Outdated Show resolved Hide resolved
@ematipico ematipico force-pushed the test/custom-tests-formatter branch from 6a74933 to 82d6fd4 Compare February 4, 2022 15:56
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SerializableFormatOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "remote derive" feature of serde might be useful for this (the facade types don't have to implement From and TestOptions can deserialize to FormatOptions directly): https://serde.rs/remote-derive.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ematipico have you tried this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and it's not possible with the data structure that I want to have.

Copy link
Contributor

@MichaReiser MichaReiser Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you seen #2045 (comment)

But agree, this isn't important. I like the name SerializableFormatOptions. It makes it clear what it is about.

@ematipico ematipico force-pushed the test/custom-tests-formatter branch from afffd5c to 293ebbc Compare February 4, 2022 17:15
crates/rome_formatter/tests/spec_test.rs Outdated Show resolved Hide resolved
crates/rome_formatter/tests/spec_test.rs Outdated Show resolved Hide resolved
crates/rome_formatter/tests/spec_test.rs Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
@ematipico
Copy link
Contributor Author

I ended up going back to first solution I implemented:

  • using serde with doesn't work well with vectors, and we would like to keep the possibility to run multiple configurations at once
  • using features is tricky and some tests might not be run

@leops
Copy link
Contributor

leops commented Feb 7, 2022

Ah yes the #[serde(with = "...")] attribute can't be directly set on a field of type Vec<_> since Vec is its own specific type, instead the elements of the Vec itself would have to to be a newtype like

#[derive(Serialize, Deserialize)]
#[serde(transparent)]
struct SerializableOptions(#[serde(with = "FormatOptionsDef")] FormatOptions);

@ematipico ematipico force-pushed the test/custom-tests-formatter branch from afa7437 to 1d11040 Compare February 7, 2022 13:17
@ematipico ematipico force-pushed the test/custom-tests-formatter branch from 1d11040 to 0a57548 Compare February 7, 2022 13:20
@ematipico ematipico requested a review from yassere February 7, 2022 13:22
@xunilrj
Copy link
Contributor

xunilrj commented Feb 7, 2022

Nice! I like the idea and code looks good to me. Let's wait for @xunilrj or @leops to chime in to see if the cfg approach is the right way to go

I would not focus too much on this now, honestly. @leops last suggestion is definitely the best, but I would go with it only on optimizing later.

@ematipico
Copy link
Contributor Author

ematipico commented Feb 8, 2022

Other than some optimizations that can be done later on, the PR is ready and it implements what it supposed it.

Can we merge it please?

@MichaReiser MichaReiser removed their request for review February 8, 2022 12:59
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SerializableFormatOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ematipico have you tried this?

@@ -4,11 +4,11 @@ mod formatter {

mod js_module {
use crate::spec_test;
tests_macros::gen_tests! {"tests/specs/js/module/**/**/*.js", spec_test::run, "module"}
tests_macros::gen_tests! {"tests/specs/js/module/**/**/**/*.js", spec_test::run, "module"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed. **/*.js should match all JS files regardless of the depth.

@@ -22,7 +103,7 @@ use std::path::Path;
///
/// * `json/null` -> input: `tests/specs/json/null.json`, expected output: `tests/specs/json/null.json.snap`
/// * `null` -> input: `tests/specs/null.json`, expected output: `tests/specs/null.json.snap`
pub fn run(spec_input_file: &str, _: &str, file_type: &str) {
pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, file_type: &str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The signature starts to become a bit awkward (so many strings). Could be worth having a SpecTestFile struct that has different attributes like input_file, expected_file, and file_type. I'm not sure what test_directory is, whatever it belongs to the file or is the same for all files.

let mut ancestors = path.ancestors();
// the first one will yield the path to the current file
ancestors.next();
let test_directory = ancestors.next().unwrap().display().to_string();
Copy link
Contributor

@MichaReiser MichaReiser Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is the test_directory. Is it the parent directory of the file? Might be easier to understand if you call path.parent()

@ematipico ematipico merged commit 1f48995 into main Feb 9, 2022
@ematipico ematipico deleted the test/custom-tests-formatter branch February 9, 2022 08:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expand testing suite with different options: different indent style and indent size.
5 participants