diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..7858035 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,67 @@ +name: test +on: + push: + branches: [main] + pull_request: + branches: [main] + +env: + CARGO_TERM_COLOR: always + +jobs: + test-linux: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: rustup toolchain install stable --profile minimal + - name: "Cache cargo" + id: cache-cargo + uses: "actions/cache@v4" + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + lib/target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- + - uses: extractions/setup-just@v2 + - run: just test + test-macos: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - run: rustup toolchain install stable --profile minimal + - name: "Cache cargo" + id: cache-cargo + uses: "actions/cache@v4" + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + lib/target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- + - uses: extractions/setup-just@v2 + - run: just test + test-windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + - name: "Cache cargo" + id: cache-cargo + uses: "actions/cache@v4" + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + lib/target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- + - uses: extractions/setup-just@v2 + - run: just test diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 8a2b7e2..a5f19b4 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -20,6 +20,9 @@ use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Cursor, Read, Seek, Write}; use std::path::{Path, PathBuf}; +const LINE_ENDING: &str = if cfg!(unix) { "\n" } else { "\r\n" }; + + fn open_file_read(file_path: &str) -> Result { let file = File::open(file_path) .map_err(|_| Error::FileError(format!("Failed to open file: {file_path}")))?; @@ -153,14 +156,17 @@ where output[0] = original_lines[0].to_string(); - let mut joined = output.join("\n"); - if original.ends_with('\n') { - if !joined.ends_with('\n') { - joined.push('\n'); + let mut joined = output.join(LINE_ENDING); + if original.ends_with(LINE_ENDING) { + if !joined.ends_with(LINE_ENDING) { + joined.push_str(LINE_ENDING); } } else { - if joined.ends_with('\n') { - joined.remove(joined.len() - 1); + if joined.ends_with(LINE_ENDING) { + joined = joined + .strip_suffix(LINE_ENDING) + .map(|x| x.to_string()) + .unwrap_or(joined); } } @@ -221,6 +227,7 @@ fn write_project(buf: &mut impl Write, element: &Element, indent: &str) -> Resul let config = EmitterConfig::new() .perform_indent(true) + .line_separator(LINE_ENDING) .indent_string(indent.to_string()); element @@ -255,11 +262,20 @@ fn write_project_to_string(element: &Element, indent: &str) -> Result(name: &str, input: T) -> Result<(), Error> { + let file_dir = if cfg!(unix) { + "/tmp/fs-tools.log".to_string() + } else { + Path::new(&std::env::var("TEMP").unwrap()) + .join("fs-tools.log") + .to_string_lossy() + .to_string() + }; + let mut file = File::options() .create(true) .write(true) .append(true) - .open("/tmp/fs-tools.log") + .open(file_dir) .map_err(Error::IOError)?; writeln!(file, "{}: {}", name, input).map_err(Error::IOError)?; diff --git a/lib/src/tests/mod.rs b/lib/src/tests/mod.rs index 4f5d906..7a072e7 100644 --- a/lib/src/tests/mod.rs +++ b/lib/src/tests/mod.rs @@ -21,6 +21,8 @@ fn find_project() { let proj = crate::find_fsproj(files_dir.join("test_file.fs").to_str().unwrap(), 1); let expected = files_dir .join("project.fsproj") + .canonicalize() + .unwrap() .to_str() .unwrap() .to_owned(); @@ -29,13 +31,14 @@ fn find_project() { } #[test] -fn find_project_nested() { +fn find_project_nested() -> AnyResult<()> { let files_dir = get_files_dir(); let test_file = files_dir.join("directory").join("inside_directory.fs"); let expected = files_dir .join("project.fsproj") + .canonicalize()? .to_str() .unwrap() .to_owned(); @@ -45,6 +48,8 @@ fn find_project_nested() { let proj = crate::find_fsproj(test_file.to_str().unwrap(), 2); assert_eq!(proj, Some(expected)); + + Ok(()) } #[test] @@ -112,7 +117,7 @@ fn set_files() -> AnyResult<()> { let fixed = fix_start_and_end(Cursor::new(result_string), Cursor::new(expected_file))?; - assert_eq!(fixed, expected_file); + core::assert_eq!(fixed, expected_file); Ok(()) }