Skip to content

Commit

Permalink
feat: support Windows and Linux-based file systems
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerziegler committed Oct 15, 2021
1 parent 419beef commit 7c18522
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ on:
push:
branches:
- main
pull_request:
branches:
- main

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: macos-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Build
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ fn main() {

// Construct the output file path.
let out_file_path = match (in_path.parent(), in_path.file_stem()) {
(Some(dir), Some(file_stem)) =>
format!("{}/{}{}", dir.to_str().unwrap_or_default(), file_stem.to_str().unwrap_or_default(), &args.output_extension),
(Some(dir), Some(file_stem)) => {
let filename_with_ext = format!("{}{}", file_stem.to_str().unwrap_or_default(), &args.output_extension);
dir.join(Path::new(&filename_with_ext))
}
_ => {
eprintln!("Could not decode file location.");
process::exit(1);
Expand Down
39 changes: 26 additions & 13 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use assert_cmd::prelude::*; // Add methods on commands
use predicates::prelude::*;
use std::path::PathBuf;
// Used for writing assertions
use std::process::Command; // Run programs
use std::path::Path;
Expand All @@ -18,18 +19,23 @@ impl fmt::Display for Failure {

impl Error for Failure {}

fn normalize_file_path(folder: &str, img: &str) -> PathBuf {
Path::new(folder).join(img)
}

#[test]
fn it_converts_files() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("imgcnvrt")?;

cmd.arg("tests/fractal.png").arg(".jpg");
let input_path = normalize_file_path("tests", "fractal.png");
let output_path = normalize_file_path("tests", "fractal.jpg");

cmd.arg(input_path.as_os_str()).arg(".jpg");
cmd.assert()
.success();

let path = Path::new("tests/fractal.jpg");

if path.is_file() {
fs::remove_file(path).unwrap();
if output_path.is_file() {
fs::remove_file(output_path).unwrap();

Ok(())
} else {
Expand All @@ -52,7 +58,9 @@ fn it_errors_if_no_file_specified() -> Result<(), Box<dyn std::error::Error>> {
fn it_errors_on_file_not_found() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("imgcnvrt")?;

cmd.arg("tests/test.jpg").arg(".png");
let input_path = normalize_file_path("tests", "test.jpg");

cmd.arg(input_path.as_os_str()).arg(".png");
cmd.assert()
.failure()
.stderr(predicate::str::contains("The provided path does not pertain to a file on disk."));
Expand All @@ -64,7 +72,9 @@ fn it_errors_on_file_not_found() -> Result<(), Box<dyn std::error::Error>> {
fn it_errors_if_no_output_extension_specified() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("imgcnvrt")?;

cmd.arg("tests/fractal.png");
let input_path = normalize_file_path("tests", "fractal.png");

cmd.arg(input_path.as_os_str());
cmd.assert()
.failure()
.stderr(predicate::str::contains("No output extension specified."));
Expand All @@ -76,7 +86,9 @@ fn it_errors_if_no_output_extension_specified() -> Result<(), Box<dyn std::error
fn it_errors_on_unsupported_extensions() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("imgcnvrt")?;

cmd.arg("tests/fractal.png").arg(".mp3");
let input_path = normalize_file_path("tests", "fractal.png");

cmd.arg(input_path.as_os_str()).arg(".mp3");
cmd.assert()
.failure()
.stderr(predicate::str::contains("Unsupported file extension."));
Expand All @@ -93,15 +105,16 @@ fn it_decodes_png_and_encodes_supported_image_formats() -> Result<(), Box<dyn st
for img_format in supported_img_formats {
let mut cmd = Command::cargo_bin("imgcnvrt")?;

cmd.arg("tests/fractal.png").arg(img_format);
let input_path = normalize_file_path("tests", "fractal.png");

cmd.arg(input_path.as_os_str()).arg(img_format);
cmd.assert()
.success();

let out_path = format!("tests/fractal{}", &img_format);
let path = Path::new(&out_path);
let output_path = normalize_file_path("tests", &format!("fractal{}", &img_format));

if path.is_file() {
fs::remove_file(path).unwrap();
if output_path.is_file() {
fs::remove_file(output_path).unwrap();
} else {
failed = true;
}
Expand Down

0 comments on commit 7c18522

Please sign in to comment.