Skip to content

Commit

Permalink
test: write tests for rust functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlimjustin committed Nov 30, 2021
1 parent 7ddf245 commit ec12eb2
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 5 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test Rust Functions
on:
push:
branches: [master]

jobs:
tests:
strategy:
fail-fast: false
matrix:
platform: [macos-latest, ubuntu-latest, windows-latest]

runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- name: setup node
uses: actions/setup-node@v1
with:
node-version: 12
- name: install Rust stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: install webkit2gtk (ubuntu only)
if: matrix.platform == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y webkit2gtk-4.0
- name: Run tests
run: yarn && yarn compile && cd src-tauri && cargo test
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn test
yarn test
cd src-tauri && cargo test
56 changes: 53 additions & 3 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ lazy_static = "1.4.0"
font-loader = "0.11.0"
glob = "0.3.0"

[dev-dependencies]
tokio = {version="1", features = ["full"] }


[features]
default = [ "custom-protocol" ]
custom-protocol = [ "tauri/custom-protocol" ]
2 changes: 1 addition & 1 deletion src-tauri/src/files_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub fn create_dir_recursive(dir_path: String) -> bool {

/// Create a file
#[tauri::command]
pub fn create_file(file_path: String) -> bool {
pub async fn create_file(file_path: String) -> bool {
fs::write(file_path, "").is_ok()
}

Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod drives;
mod file_lib;
mod files_api;
mod storage;
mod tests;
use font_loader::system_fonts;
use std::env;
#[cfg(target_os = "windows")]
Expand Down
131 changes: 131 additions & 0 deletions src-tauri/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#[cfg(test)]
mod tests {
use crate::files_api::*;
extern crate path_absolutize;
use path_absolutize::*;
#[test]
fn test_get_basename() {
assert_eq!(get_basename("/home/user/file.txt".to_string()), "file.txt");
assert_eq!(get_basename("/home/user/file.txt/".to_string()), "file.txt");
assert_eq!(get_basename("C://a/b/c".to_string()), "c");
}

#[cfg(unix)]
#[test]
fn test_is_hidden() {
assert_eq!(
check_is_hidden("/home/user/.hidden_file.txt".to_string()),
true
);
assert_eq!(check_is_hidden("/home/user/file.txt".to_string()), false);
}

#[cfg(windows)]
#[test]
fn test_is_hidden() {
assert_eq!(
check_is_hidden(
std::fs::canonicalize(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.git"))
.unwrap()
.to_str()
.unwrap()
.to_string()
),
true
);
}

#[test]
fn test_is_dir() {
assert_eq!(
is_dir(std::path::Path::new(env!("CARGO_MANIFEST_DIR"))).unwrap(),
true
);
assert_eq!(
is_dir(&std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("cargo.toml")).unwrap(),
false
);
}
#[test]
fn test_file_exist() {
assert_eq!(
file_exist(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.to_str()
.unwrap()
.to_string()
),
true
);
assert_eq!(
file_exist(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("Cargo.toml")
.to_str()
.unwrap()
.to_string()
),
true
);
assert_eq!(
file_exist(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("cargo.toml.bak")
.to_str()
.unwrap()
.to_string()
),
false
);
}
#[test]
fn test_create_dir_recursive() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../temp/a/b")
.to_str()
.unwrap()
.to_string();
create_dir_recursive(dir.clone());
assert_eq!(is_dir(std::path::Path::new(dir.as_str())).unwrap(), true);
assert_eq!(file_exist(dir.clone()), true);
}
#[tokio::test]
async fn test_create_file() {
let file = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../temp/a/b/c.txt")
.absolutize()
.unwrap()
.into_owned()
.into_os_string()
.into_string()
.unwrap();
create_file(file.clone()).await;
assert_eq!(file_exist(file.clone()), true);
assert_eq!(is_dir(std::path::Path::new(file.as_str())).unwrap(), false);
}

#[test]
fn test_delete_file() {
let file = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../temp/a/b/c.txt")
.to_str()
.unwrap()
.to_string();
let mut files = Vec::new();
files.push(file.clone());
delete_file(files);
assert_eq!(file_exist(file.clone()), false);
}
#[test]
fn test_delete_dir() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../temp")
.to_str()
.unwrap()
.to_string();
let mut dirs = Vec::new();
dirs.push(dir.clone());
delete_file(dirs);
assert_eq!(file_exist(dir.clone()), false);
}
}

1 comment on commit ec12eb2

@vercel
Copy link

@vercel vercel bot commented on ec12eb2 Nov 30, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.