Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
30 changes: 23 additions & 7 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedFileLock, Error> {
let file = File::open(file_path)
.map_err(|_| Error::FileError(format!("Failed to open file: {file_path}")))?;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -255,11 +262,20 @@ fn write_project_to_string(element: &Element, indent: &str) -> Result<String, Er

#[cfg(debug_assertions)]
fn write_log<T: std::fmt::Display>(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)?;
Expand Down
9 changes: 7 additions & 2 deletions lib/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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]
Expand Down Expand Up @@ -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(())
}
Expand Down
Loading