Skip to content

Commit

Permalink
fix(windows): fixes newline management (#11)
Browse files Browse the repository at this point in the history
Windows Server still uses \r\n for newlines. That's not the case for Windows 10 or 11.

This adds support for \r\n. It adds also new test cases and adds windows and mac to the ci matrix to avoid issues like this in the future.

fix #10
  • Loading branch information
robertohuertasm authored Nov 26, 2022
1 parent b989832 commit c105669
Show file tree
Hide file tree
Showing 4 changed files with 22,266 additions and 59 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@ jobs:
fail-fast: true
matrix:
rust: [stable]
os: [ubuntu-latest] # we could add macOS-latest & windows-latest but don't need it for the moment
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- name: Setup Rust
- name: Setup Rust (with fmt and clippy)
uses: hecrj/setup-rust-action@master
with:
rust-version: ${{ matrix.rust }}
components: rustfmt, clippy
- name: Install cargo-make
uses: actions-rs/cargo@v1
with:
command: install
args: --debug cargo-make
- name: Checkout
uses: actions/checkout@v2
- name: Run cargo fmt
run: cargo make format
if: matrix.os == 'ubuntu-latest'
run: cargo fmt --all -- --check
- name: Run cargo clippy
run: cargo make clippy
if: matrix.os == 'ubuntu-latest'
run: cargo clippy
- name: Run tests
run: cargo make test
run: cargo test
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "yarn-lock-parser"
description = "yarn.lock parser"
version = "0.4.0"
version = "0.4.1"
authors = [
"Roberto Huertas <roberto.huertas@outlook.com>",
"Riccardo Attilio Galli <riccardo@sideralis.org>"
Expand Down
167 changes: 119 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nom::{
branch::alt,
bytes::complete::{is_not, tag, take, take_till, take_until},
character::{
complete::{line_ending, newline, not_line_ending, one_of, space0, space1},
complete::{line_ending, not_line_ending, one_of, space0, space1},
streaming::multispace0,
},
combinator::{eof, fail, opt, recognize},
Expand Down Expand Up @@ -66,14 +66,14 @@ fn parse(input: &str) -> Res<&str, Vec<Entry>> {

fn take_till_line_end(input: &str) -> Res<&str, &str> {
recognize(tuple((
alt((take_until("\n"), take_until("\n\r"))),
alt((take_until("\n"), take_until("\r\n"))),
take(1usize),
)))(input)
}

fn take_till_optional_line_end(input: &str) -> Res<&str, &str> {
recognize(tuple((
alt((take_until("\n"), take_until("\n\r"), space0)),
alt((take_until("\n"), take_until("\r\n"), space0)),
take(1usize),
)))(input)
}
Expand All @@ -88,7 +88,7 @@ fn yarn_lock_metadata(input: &str) -> Res<&str, &str> {
recognize(tuple((
tag("__metadata:"),
take_till_line_end,
many_till(take_till_line_end, recognize(tuple((space0, newline)))),
many_till(take_till_line_end, recognize(tuple((space0, line_ending)))),
multispace0,
))),
)(input)
Expand All @@ -104,7 +104,7 @@ fn entry_final(input: &str) -> Res<&str, Entry> {
fn entry(input: &str) -> Res<&str, Entry> {
recognize(many_till(
take_till_line_end,
recognize(tuple((space0, newline))),
recognize(tuple((space0, line_ending))),
))(input)
.map(|(i, capture)| {
let (_, my_entry) = parse_entry(capture).expect("Error parsing Entry");
Expand Down Expand Up @@ -291,6 +291,7 @@ fn entry_descriptors<'a>(input: &'a str) -> Res<&str, Vec<(&str, &str)>> {
}

fn entry_version(input: &str) -> Res<&str, EntryItem> {
// "version \"7.12.13\"\r\n"
context(
"version",
tuple((
Expand Down Expand Up @@ -366,6 +367,13 @@ mod tests {
);
}

#[test]
fn parse_windows_server_from_memory_works() {
let content = "# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\r\n# yarn lockfile v1\r\n\r\n\r\n\"@babel/code-frame@^7.0.0\":\r\n version \"7.12.13\"\r\n resolved \"https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658\"\r\n integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==\r\n dependencies:\r\n \"@babel/highlight\" \"^7.12.13\"\r\n\r\nyargs-parser@^7.0.0:\r\n version \"7.0.0\"\r\n resolved \"https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-7.0.0.tgz#8d0ac42f16ea55debd332caf4c4038b3e3f5dfd9\"\r\n integrity sha1-jQrELxbqVd69MyyvTEA4s+P139k=\r\n dependencies:\r\n camelcase \"^4.1.0\"\r\n\r\nyargs@^9.0.0:\r\n version \"9.0.1\"\r\n resolved \"https://registry.yarnpkg.com/yargs/-/yargs-9.0.1.tgz#52acc23feecac34042078ee78c0c007f5085db4c\"\r\n integrity sha1-UqzCP+7Kw0BCB47njAwAf1CF20w=\r\n dependencies:\r\n camelcase \"^4.1.0\"\r\n cliui \"^3.2.0\"\r\n decamelize \"^1.1.1\"\r\n get-caller-file \"^1.0.1\"\r\n os-locale \"^2.0.0\"\r\n read-pkg-up \"^2.0.0\"\r\n require-directory \"^2.1.1\"\r\n require-main-filename \"^1.0.1\"\r\n set-blocking \"^2.0.0\"\r\n string-width \"^2.0.0\"\r\n which-module \"^2.0.0\"\r\n y18n \"^3.2.1\"\r\n yargs-parser \"^7.0.0\"\r\n";
let res = parse(&content).unwrap();
assert_v1(res);
}

#[test]
fn parse_v1_doc_from_file_works() {
let content = std::fs::read_to_string("tests/v1/yarn.lock").unwrap();
Expand Down Expand Up @@ -613,54 +621,80 @@ __metadata:
}

assert(
r#""@babel/code-frame@^7.0.0":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658"
integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==
dependencies:
"@babel/highlight" "^7.12.13"
"#,
"\"@babel/code-frame@^7.0.0\":\r\n version \"7.12.13\"\r\n resolved \"https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658\"\r\n integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==\r\n dependencies:\r\n \"@babel/highlight\" \"^7.12.13\"\r\n\r\n",
Entry {
name: "@babel/code-frame",
version: "7.12.13",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g=="
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
},
);

assert(
"\"@babel/code-frame@^7.0.0\":\n version \"7.12.13\"\n resolved \"https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658\"\n integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==\n dependencies:\n \"@babel/highlight\" \"^7.12.13\"\n\n",
Entry {
name: "@babel/code-frame",
version: "7.12.13",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
},
);

assert(
r#""@babel/code-frame@^7.0.0":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658"
integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==
dependencies:
"@babel/highlight" "^7.12.13"
"#,
Entry {
name: "@babel/code-frame",
version: "7.12.13",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g=="
},
);

// with final spaces
assert(
r#""@babel/helper-validator-identifier@^7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed"
integrity sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==
r#""@babel/helper-validator-identifier@^7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed"
integrity sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==
"#,
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
},
);

"#,
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
},
);
// without final spaces
assert(
r#""@babel/helper-validator-identifier@^7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed"
integrity sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==
r#""@babel/helper-validator-identifier@^7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed"
integrity sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==
"#,
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
},
);
"#,
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
},
);
}

#[test]
Expand All @@ -669,16 +703,22 @@ __metadata:
let res = parse_entry(input).unwrap();
assert_eq!(res.1, expect);
}

// escaped lines
assert(
r#""@babel/code-frame@^7.0.0":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658"
integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==
dependencies:
"@babel/highlight" "^7.12.13"
"\"@babel/code-frame@^7.0.0\":\n version \"7.12.13\"\n resolved \"https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658\"\n integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==\n dependencies:\n \"@babel/highlight\" \"^7.12.13\"\n\n",
Entry {
name: "@babel/code-frame",
version: "7.12.13",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
},
);

"#,
// escaped lines windows
assert(
"\"@babel/code-frame@^7.0.0\":\r\n version \"7.12.13\"\r\n resolved \"https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658\"\r\n integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==\r\n dependencies:\r\n \"@babel/highlight\" \"^7.12.13\"\r\n\r\n",
Entry {
name: "@babel/code-frame",
version: "7.12.13",
Expand All @@ -688,6 +728,26 @@ __metadata:
..Default::default()
},
);

// normal
assert(
r#""@babel/code-frame@^7.0.0":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658"
integrity sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==
dependencies:
"@babel/highlight" "^7.12.13"
"#,
Entry {
name: "@babel/code-frame",
version: "7.12.13",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
},
);
}

#[test]
Expand Down Expand Up @@ -717,6 +777,10 @@ __metadata:

#[test]
fn entry_version_works() {
assert_eq!(
entry_version(" version \"1.2.3\"\r\n"),
Ok(("", EntryItem::Version("1.2.3")))
);
assert_eq!(
entry_version(" version \"1.2.3\"\n"),
Ok(("", EntryItem::Version("1.2.3")))
Expand Down Expand Up @@ -852,4 +916,11 @@ __metadata:
EntryItem::Dependencies(vec![("foo", "1.0 || 2.0"), ("bar", "0.3-alpha1")]),
);
}

#[test]
fn take_till_the_end_works() {
let k = take_till_line_end("foo\r\nbar").unwrap();
assert_eq!(k.0, "bar");
assert_eq!(k.1, "foo\r\n");
}
}
Loading

0 comments on commit c105669

Please sign in to comment.