Selaa lähdekoodia

feat(Tauri-core) Add Quicktest to Tauri-API (#239)

* add simple bat and ps1 setup scripts

* add proptest for property testing

* add comments.

* add basic bash script (needs improvement)

* add basic quickcheck tests.

* add more comments.

* add simple is_dir test.

* add support for windows commands; remove returns

* remove print statement

* change prefixes to ch and add script

* fix prefixes.

* move qc script to .scripts folder.

* fix bash path.

* move scripts to root and add if checks.

* update bash script with more logic to check dirs.

* update bash script with more logic to check dirs.

* clean up and verify

* update gitignore for .vscode folder outside root

* add docs

* update docs

* format scripts
Tensor-Programming 5 vuotta sitten
vanhempi
sitoutus
83b3fcb8c8

+ 1 - 0
.gitignore

@@ -59,6 +59,7 @@ typings/
 debug.log
 package-lock.json
 .vscode/settings.json
+*/.vscode/
 
 # Tauri output
 /bundle.json

+ 28 - 0
.scripts/init_env.bat

@@ -0,0 +1,28 @@
+@echo OFF
+echo "Setting up enviromental Variables"
+
+rem check script execution directory vs script directory. 
+
+IF "%cd%\"=="%~dp0" (
+    GOTO exitnodir
+)
+
+rem setup relative paths from root folder
+set "TAURI_DIST_DIR=%~1tauri\test\fixture\dist"
+set "TAURI_DIR=%~1tauri\test\fixture\src-tauri"
+rem convert relative path to absolute path and re-set it into the enviroment var
+for /F "delims=" %%F IN ("%TAURI_DIST_DIR%") DO SET "TAURI_DIST_DIR=%%~fF"
+for /F "delims=" %%F IN ("%TAURI_DIR%") DO SET "TAURI_DIR=%%~fF"
+
+if NOT EXIST %TAURI_DIR% GOTO exitnodir
+if NOT EXIST %TAURI_DIST_DIR% GOTO exitnodir
+
+GOTO exitfine
+
+:exitnodir
+echo "Variables are not setup properly. Please run from Tauri Root directory"
+@EXIT /B 1
+
+:exitfine
+echo "Variables set, ready to work!"
+@EXIT /B 0

+ 17 - 0
.scripts/init_env.ps1

@@ -0,0 +1,17 @@
+Write-Output "Setting up enviromental Variables"
+# setup relative paths
+$dist_path = "tauri\test\fixture\dist"
+$src_path = "tauri\test\fixture\src-tauri"
+
+# check to see if path variables are directories
+if ((Test-Path $dist_path -PathType Any) -Or (Test-Path $src_path -PathType Any)) {
+    # convert relative paths to absolute paths. 
+    # put these absolute paths in enviromental variables
+    $env:TAURI_DIST_DIR = Resolve-Path $dist_path
+    $env:TAURI_DIR = Resolve-Path $src_path 
+    Write-Output "Variables set, ready to work!"
+    
+}
+else {
+    Write-Output "Variables are not setup properly. Please run from Tauri Root directory"
+}

+ 25 - 0
.scripts/init_env.sh

@@ -0,0 +1,25 @@
+#!/bin/sh
+# Note: Script must be run like this `. .init_env.sh` to setup variables for your current shell
+# define relative paths
+
+DistPath="tauri/test/fixture/dist"
+SrcPath="tauri/test/fixture/src-tauri"
+
+echo "Setting up enviromental Variables"
+
+# check if relative paths exist
+if [ -d "$DistPath" ]||[ -d "$SrcPath" ]
+    then
+        # Convert to absolute paths
+        DistPath="$(cd "$DistPath" && pwd -P)"
+        SrcPath="$(cd "$SrcPath" && pwd -P)"
+
+        # export enviromental variables 
+        export TAURI_DIST_DIR=$DistPath
+        export TAURI_DIR=$SrcPath
+        echo "Variables set, ready to work!"
+    
+else
+    # if directories don't exist then exit script and tell user run script in root dir. 
+    echo "Error: Variables are not setup properly. Please run from Tauri Root directory '. .scripts/init_env.sh'"
+fi

+ 10 - 0
tauri-api/.scripts/loop_qc.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# Loop all quickcheck tests for tauri-api. 
+while true
+do
+    cargo test qc_
+    if [[ x$? != x0 ]] ; then
+        exit $?
+    fi
+done

+ 4 - 0
tauri-api/Cargo.toml

@@ -20,3 +20,7 @@ tempfile = "3"
 either = "1.5.3"
 tar = "0.4"
 flate2 = "1"
+
+[dev-dependencies]
+quickcheck = "0.8.0"
+quickcheck_macros = "0.8.0"

+ 15 - 12
tauri-api/src/command.rs

@@ -8,16 +8,19 @@ pub fn get_output(cmd: String, args: Vec<String>, stdout: Stdio) -> Result<Strin
     .map_err(|err| err.to_string())
     .and_then(|output| {
       if output.status.success() {
-        return Result::Ok(String::from_utf8_lossy(&output.stdout).to_string());
+        Result::Ok(String::from_utf8_lossy(&output.stdout).to_string())
       } else {
-        return Result::Err(String::from_utf8_lossy(&output.stderr).to_string());
+        Result::Err(String::from_utf8_lossy(&output.stderr).to_string())
       }
     })
 }
 
-// TODO use .exe for windows builds
 pub fn format_command(path: String, command: String) -> String {
-  return format!("{}/./{}", path, command);
+  if cfg!(windows) {
+    format!("{}/./{}.exe", path, command)
+  } else {
+    format!("{}/./{}", path, command)
+  }
 }
 
 pub fn relative_command(command: String) -> Result<String, std::io::Error> {
@@ -32,16 +35,16 @@ pub fn relative_command(command: String) -> Result<String, std::io::Error> {
   }
 }
 
-// TODO append .exe for windows builds
 pub fn command_path(command: String) -> Result<String, std::io::Error> {
   match std::env::current_exe()?.parent() {
-    Some(exe_dir) => return Ok(format!("{}/{}", exe_dir.display().to_string(), command)),
-    None => {
-      return Err(std::io::Error::new(
-        std::io::ErrorKind::Other,
-        "Could not evaluate executable dir".to_string(),
-      ))
-    }
+    #[cfg(not(windows))]
+    Some(exe_dir) => Ok(format!("{}/{}", exe_dir.display().to_string(), command)),
+    #[cfg(windows)]
+    Some(exe_dir) => Ok(format!("{}/{}.exe", exe_dir.display().to_string(), command)),
+    None => Err(std::io::Error::new(
+      std::io::ErrorKind::Other,
+      "Could not evaluate executable dir".to_string(),
+    )),
   }
 }
 

+ 17 - 0
tauri-api/src/dir.rs

@@ -75,3 +75,20 @@ pub fn with_temp_dir<F: FnOnce(&tempfile::TempDir) -> ()>(
   dir.close()?;
   Ok(())
 }
+
+#[cfg(test)]
+mod test {
+  use crate::dir::*;
+
+  // check is dir function by passing in arbitrary strings
+  #[quickcheck]
+  fn qc_is_dir(f: String) -> bool {
+    // is the string runs through is_dir and comes out as an OK result then it must be a DIR.
+    match is_dir(f.clone()) {
+      // check to see that the path exists.
+      Ok(_) => std::path::PathBuf::from(f).exists(),
+      // if is Err then string isn't a path nor a dir and function passes.
+      Err(_) => true,
+    }
+  }
+}

+ 6 - 0
tauri-api/src/lib.rs

@@ -1,3 +1,9 @@
+#[cfg(test)]
+extern crate quickcheck;
+#[cfg(test)]
+#[macro_use(quickcheck)]
+extern crate quickcheck_macros;
+
 pub mod command;
 pub mod dir;
 pub mod file;

+ 58 - 0
tauri-api/src/rpc.rs

@@ -13,3 +13,61 @@ pub fn format_callback_result(
     Err(err) => return format_callback(error_callback, format!("\"{}\"", err)),
   }
 }
+
+#[cfg(test)]
+mod test {
+  use crate::rpc::*;
+
+  // check abritrary strings in the format callback function
+  #[quickcheck]
+  fn qc_formating(f: String, a: String) -> bool {
+    // can not accept empty strings
+    if f != "" && a != "" {
+      // get length of function and argument
+      let alen = &a.len();
+      let flen = &f.len();
+      // call format callback
+      let fc = format_callback(f, a);
+      // get length of the resulting string
+      let fclen = fc.len();
+
+      // if formatted string equals the length of the argument and the function plus 12 then its correct.
+      fclen == alen + flen + 12
+    } else {
+      true
+    }
+  }
+
+  // check arbitrary strings in format_callback_result
+  #[quickcheck]
+  fn qc_format_res(result: Result<String, String>, c: String, ec: String) -> bool {
+    // match on result to decide how to call the function.
+    match result {
+      // if ok, get length of result and callback strings.
+      Ok(r) => {
+        let rlen = r.len();
+        let clen = c.len();
+
+        // take the ok string from result and pass it into format_callback_result as an ok.
+        let resp = format_callback_result(Ok(r), c, ec);
+        // get response string length
+        let reslen = resp.len();
+
+        // if response string length equals result and callback length plus 12 characters then it is correct.
+        reslen == rlen + clen + 12
+      }
+      // If Err, get length of Err and Error callback
+      Err(err) => {
+        let eclen = ec.len();
+        let errlen = err.len();
+        // pass err as Err into format_callback_result with callback and error callback
+        let resp = format_callback_result(Err(err), c, ec);
+        // get response string length
+        let reslen = resp.len();
+
+        // if length of response string equals the error length and the error callback length plus 14 characters then its is correct.
+        reslen == eclen + errlen + 14
+      }
+    }
+  }
+}

+ 1 - 0
tauri/Cargo.toml

@@ -28,6 +28,7 @@ tauri-api = { version = "0.2",  path = "../tauri-api" }
 [build-dependencies]
 tauri_includedir_codegen = "0.5.1"
 
+
 [features]
 edge = ["web-view/edge"]
 dev-server = []