소스 검색

feat(workflow) improve clippy check, add fmt and audit checks (#754)

Lucas Fernandes Nogueira 5 년 전
부모
커밋
c8f430297f

+ 18 - 0
.github/workflows/audit.yml

@@ -0,0 +1,18 @@
+name: Audit
+
+on:
+  schedule:
+    - cron: '0 0 * * *'
+  push:
+    paths:
+      - "**/Cargo.lock"
+      - "**/Cargo.toml"
+
+jobs:
+  audit-rust:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+      - uses: actions-rs/audit-check@v1
+        with:
+          token: ${{ secrets.GITHUB_TOKEN }}

+ 36 - 2
.github/workflows/check-on-push.yml

@@ -1,4 +1,4 @@
-name: clippy and eslint check
+name: clippy, fmt and eslint check
 
 on:
   push:
@@ -7,7 +7,7 @@ on:
       - dev
 
 jobs:
-  clippy_check:
+  workspace_clippy_fmt_check:
     runs-on: ubuntu-latest
 
     steps:
@@ -20,6 +20,40 @@ jobs:
       - uses: actions-rs/clippy-check@v1
         with:
           token: ${{ secrets.GITHUB_TOKEN }}
+          args: --all-targets -- -D warnings
+          name: workspace
+        env:
+          TAURI_DIST_DIR: ${{ runner.workspace }}/tauri/tauri/examples/communication/dist
+          TAURI_DIR: ${{ runner.workspace }}/tauri/tauri/examples/communication/src-tauri
+      - uses: actions-rs/toolchain@v1
+        with:
+            profile: minimal
+            toolchain: nightly
+            override: true
+            components: rustfmt
+      - uses: actions-rs/cargo@v1
+        with:
+          command: fmt
+          args: --all -- --check
+
+  core_clippy_check:
+    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        feature: [embedded-server, no-server, all-api]
+
+    steps:
+      - uses: actions/checkout@v2
+      - name: install webkit2gtk
+        run: |
+          sudo apt-get update
+          sudo apt-get install -y webkit2gtk-4.0
+      - run: rustup component add clippy
+      - uses: actions-rs/clippy-check@v1
+        with:
+          token: ${{ secrets.GITHUB_TOKEN }}
+          args: --manifest-path ./tauri/Cargo.toml --all-targets --features ${{ matrix.feature }} -- -D warnings
+          name: core
         env:
           TAURI_DIST_DIR: ${{ runner.workspace }}/tauri/tauri/examples/communication/dist
           TAURI_DIR: ${{ runner.workspace }}/tauri/tauri/examples/communication/src-tauri

+ 0 - 1
tauri-api/Cargo.toml

@@ -39,7 +39,6 @@ once_cell = "1.4.0"
 [dev-dependencies]
 quickcheck = "0.9.2"
 quickcheck_macros = "0.9.1"
-totems = "0.2.7"
 
 [features]
 cli = [ "clap" ]

+ 36 - 32
tauri-api/src/cli.rs

@@ -1,4 +1,4 @@
-use crate::config::{get as get_config, CliConfig};
+use crate::config::{get as get_config, CliArg, CliConfig};
 
 use clap::{App, Arg, ArgMatches};
 use serde::Serialize;
@@ -59,7 +59,7 @@ pub fn get_matches() -> crate::Result<Matches> {
     .tauri
     .cli
     .as_ref()
-    .ok_or(anyhow::anyhow!("CLI configuration not defined"))?;
+    .ok_or_else(|| anyhow::anyhow!("CLI configuration not defined"))?;
 
   let about = cli
     .description()
@@ -138,36 +138,7 @@ fn get_app<'a>(name: &str, about: Option<&'a String>, config: &'a CliConfig) ->
   if let Some(args) = config.args() {
     for arg in args {
       let arg_name = arg.name.as_ref();
-      let mut clap_arg = Arg::new(arg_name).long(arg_name);
-
-      if let Some(short) = arg.short {
-        clap_arg = clap_arg.short(short);
-      }
-
-      clap_arg = bind_string_arg!(arg, clap_arg, description, about);
-      clap_arg = bind_string_arg!(arg, clap_arg, long_description, long_about);
-      clap_arg = bind_value_arg!(arg, clap_arg, takes_value);
-      clap_arg = bind_value_arg!(arg, clap_arg, multiple);
-      clap_arg = bind_value_arg!(arg, clap_arg, multiple_occurrences);
-      clap_arg = bind_value_arg!(arg, clap_arg, number_of_values);
-      clap_arg = bind_string_slice_arg!(arg, clap_arg, possible_values);
-      clap_arg = bind_value_arg!(arg, clap_arg, min_values);
-      clap_arg = bind_value_arg!(arg, clap_arg, max_values);
-      clap_arg = bind_string_arg!(arg, clap_arg, required_unless, required_unless);
-      clap_arg = bind_value_arg!(arg, clap_arg, required);
-      clap_arg = bind_string_arg!(arg, clap_arg, required_unless, required_unless);
-      clap_arg = bind_string_slice_arg!(arg, clap_arg, required_unless_all);
-      clap_arg = bind_string_slice_arg!(arg, clap_arg, required_unless_one);
-      clap_arg = bind_string_arg!(arg, clap_arg, conflicts_with, conflicts_with);
-      clap_arg = bind_string_slice_arg!(arg, clap_arg, conflicts_with_all);
-      clap_arg = bind_string_arg!(arg, clap_arg, requires, requires);
-      clap_arg = bind_string_slice_arg!(arg, clap_arg, requires_all);
-      clap_arg = bind_if_arg!(arg, clap_arg, requires_if);
-      clap_arg = bind_if_arg!(arg, clap_arg, required_if);
-      clap_arg = bind_value_arg!(arg, clap_arg, require_equals);
-      clap_arg = bind_value_arg!(arg, clap_arg, index);
-
-      app = app.arg(clap_arg);
+      app = app.arg(get_arg(arg_name, &arg));
     }
   }
 
@@ -180,3 +151,36 @@ fn get_app<'a>(name: &str, about: Option<&'a String>, config: &'a CliConfig) ->
 
   app
 }
+
+fn get_arg<'a>(arg_name: &'a str, arg: &'a CliArg) -> Arg<'a> {
+  let mut clap_arg = Arg::new(arg_name).long(arg_name);
+
+  if let Some(short) = arg.short {
+    clap_arg = clap_arg.short(short);
+  }
+
+  clap_arg = bind_string_arg!(arg, clap_arg, description, about);
+  clap_arg = bind_string_arg!(arg, clap_arg, long_description, long_about);
+  clap_arg = bind_value_arg!(arg, clap_arg, takes_value);
+  clap_arg = bind_value_arg!(arg, clap_arg, multiple);
+  clap_arg = bind_value_arg!(arg, clap_arg, multiple_occurrences);
+  clap_arg = bind_value_arg!(arg, clap_arg, number_of_values);
+  clap_arg = bind_string_slice_arg!(arg, clap_arg, possible_values);
+  clap_arg = bind_value_arg!(arg, clap_arg, min_values);
+  clap_arg = bind_value_arg!(arg, clap_arg, max_values);
+  clap_arg = bind_string_arg!(arg, clap_arg, required_unless, required_unless);
+  clap_arg = bind_value_arg!(arg, clap_arg, required);
+  clap_arg = bind_string_arg!(arg, clap_arg, required_unless, required_unless);
+  clap_arg = bind_string_slice_arg!(arg, clap_arg, required_unless_all);
+  clap_arg = bind_string_slice_arg!(arg, clap_arg, required_unless_one);
+  clap_arg = bind_string_arg!(arg, clap_arg, conflicts_with, conflicts_with);
+  clap_arg = bind_string_slice_arg!(arg, clap_arg, conflicts_with_all);
+  clap_arg = bind_string_arg!(arg, clap_arg, requires, requires);
+  clap_arg = bind_string_slice_arg!(arg, clap_arg, requires_all);
+  clap_arg = bind_if_arg!(arg, clap_arg, requires_if);
+  clap_arg = bind_if_arg!(arg, clap_arg, required_if);
+  clap_arg = bind_value_arg!(arg, clap_arg, require_equals);
+  clap_arg = bind_value_arg!(arg, clap_arg, index);
+
+  clap_arg
+}

+ 4 - 5
tauri-api/src/command.rs

@@ -95,7 +95,6 @@ mod test {
   use super::*;
   use crate::Error;
   use std::io;
-  use totems::{assert_err, assert_ok};
 
   #[test]
   // test the get_output function with a unix cat command.
@@ -107,7 +106,7 @@ mod test {
     let res = get_output(cmd, vec!["test/test.txt".to_string()], Stdio::piped());
 
     // assert that the result is an Ok() type
-    assert_ok!(&res);
+    assert!(res.is_ok());
 
     // if the assertion passes, assert the incoming data.
     if let Ok(s) = &res {
@@ -126,7 +125,7 @@ mod test {
     let res = get_output(cmd, vec!["test/".to_string()], Stdio::piped());
 
     // assert that the result is an Error type.
-    assert_err!(&res);
+    assert!(res.is_err());
 
     // destruct the Error to check the ErrorKind and test that it is a Command type.
     if let Some(Error::Command(e)) = res.unwrap_err().downcast_ref::<Error>() {
@@ -145,7 +144,7 @@ mod test {
     let res = command_path(cmd);
 
     // assert that the result is an OK() type.
-    assert_ok!(res);
+    assert!(res.is_ok());
   }
 
   #[test]
@@ -158,7 +157,7 @@ mod test {
     let res = spawn_relative_command(cmd, vec!["test/test.txt".to_string()], Stdio::piped());
 
     // this fails because there is no cat binary in the relative parent folder of this current executing command.
-    assert_err!(&res);
+    assert!(res.is_err());
 
     // after asserting that the result is an error, check that the error kind is ErrorKind::Io
     if let Some(s) = res.unwrap_err().downcast_ref::<io::Error>() {

+ 1 - 1
tauri-api/src/config.rs

@@ -404,7 +404,7 @@ mod test {
         println!("{:?}", c);
         assert_eq!(c, &test_config)
       }
-      Err(_) => assert!(false),
+      Err(e) => panic!("get config failed: {:?}", e.to_string()),
     }
   }
 

+ 5 - 6
tauri-api/src/dir.rs

@@ -66,13 +66,12 @@ mod test {
   use quickcheck_macros::quickcheck;
   use std::ffi::OsStr;
   use std::path::PathBuf;
-  use totems::assert_ok;
 
   // check is dir function by passing in arbitrary strings
   #[quickcheck]
   fn qc_is_dir(f: String) -> bool {
     // if the string runs through is_dir and comes out as an OK result then it must be a DIR.
-    if let Ok(_) = is_dir(f.clone()) {
+    if is_dir(f.clone()).is_ok() {
       PathBuf::from(f).is_dir()
     } else {
       true
@@ -98,10 +97,10 @@ mod test {
     file_two.push("test_binary");
 
     // call walk_dir on the directory
-    let res = read_dir(dir.clone(), true);
+    let res = read_dir(dir, true);
 
     // assert that the result is Ok()
-    assert_ok!(&res);
+    assert!(res.is_ok());
 
     // destruct the OK into a vector of DiskEntry Structs
     if let Ok(vec) = res {
@@ -147,7 +146,7 @@ mod test {
     let res = read_dir(dir, false);
 
     // assert that the result is Ok()
-    assert_ok!(&res);
+    assert!(res.is_ok());
 
     // destruct the vector from the Ok()
     if let Ok(vec) = res {
@@ -194,6 +193,6 @@ mod test {
     let res = with_temp_dir(callback);
 
     // assert that the result is an OK type.
-    assert_ok!(res);
+    assert!(res.is_ok());
   }
 }

+ 4 - 5
tauri-api/src/file.rs

@@ -27,7 +27,6 @@ pub fn read_binary<P: AsRef<Path>>(file: P) -> crate::Result<Vec<u8>> {
 mod test {
   use super::*;
   use crate::Error;
-  use totems::{assert_err, assert_ok};
 
   #[test]
   fn check_read_string() {
@@ -35,7 +34,7 @@ mod test {
 
     let res = read_string(file);
 
-    assert_ok!(res);
+    assert!(res.is_ok());
 
     if let Ok(s) = res {
       assert_eq!(s, "This is a test doc!".to_string());
@@ -48,7 +47,7 @@ mod test {
 
     let res = read_string(file);
 
-    assert_err!(res);
+    assert!(res.is_err());
 
     if let Some(Error::File(e)) = res.unwrap_err().downcast_ref::<Error>() {
       #[cfg(windows)]
@@ -81,7 +80,7 @@ mod test {
 
     let res = read_binary(file);
 
-    assert_ok!(res);
+    assert!(res.is_ok());
 
     if let Ok(vec) = res {
       assert_eq!(vec, expected_vec);
@@ -94,7 +93,7 @@ mod test {
 
     let res = read_binary(file);
 
-    assert_err!(res);
+    assert!(res.is_err());
 
     if let Some(Error::File(e)) = res.unwrap_err().downcast_ref::<Error>() {
       #[cfg(windows)]

+ 4 - 0
tauri/build.rs

@@ -84,6 +84,10 @@ fn shared() {
     println!("cargo:rerun-if-changed={:?}", tauri_path);
   }
 
+  setup_env_aliases();
+}
+
+fn setup_env_aliases() {
   cfg_aliases! {
     embedded_server: { feature = "embedded-server" },
     no_server: { feature = "no-server" },

+ 25 - 32
tauri/src/app/runner.rs

@@ -47,7 +47,7 @@ pub(crate) fn run(application: &mut App) -> crate::Result<()> {
 
   // spawn the embedded server on our server url
   #[cfg(embedded_server)]
-  spawn_server(server_url.to_string())?;
+  spawn_server(server_url)?;
 
   // spin up the updater process
   #[cfg(feature = "updater")]
@@ -89,7 +89,7 @@ fn setup_content() -> crate::Result<Content<String>> {
   })
   .expect("Unable to setup URL");
 
-  Ok(Content::Url(url.to_string()))
+  Ok(Content::Url(url))
 }
 
 // setup content for no-server
@@ -113,7 +113,7 @@ fn setup_port() -> crate::Result<(String, bool)> {
     let port_valid = port_is_available(
       port
         .parse::<u16>()
-        .expect(&format!("Invalid port {}", port)),
+        .unwrap_or_else(|_| panic!("Invalid port {}", port)),
     );
     Ok((port.to_string(), port_valid))
   }
@@ -134,13 +134,8 @@ fn setup_server_url(port: String) -> crate::Result<String> {
 #[cfg(embedded_server)]
 fn spawn_server(server_url: String) -> crate::Result<()> {
   spawn(move || {
-    let server = tiny_http::Server::http(
-      server_url
-        .clone()
-        .replace("http://", "")
-        .replace("https://", ""),
-    )
-    .expect("Unable to spawn server");
+    let server = tiny_http::Server::http(server_url.replace("http://", "").replace("https://", ""))
+      .expect("Unable to spawn server");
     for request in server.incoming_requests() {
       let url = match request.url() {
         "/" => "/index.tauri.html",
@@ -271,19 +266,14 @@ fn get_api_error_message(arg: &str, handler_error_message: String) -> String {
 #[cfg(test)]
 mod test {
   use proptest::prelude::*;
+  use std::env;
   use web_view::Content;
 
   #[cfg(not(feature = "embedded-server"))]
-  use std::{env, fs::read_to_string, path::Path};
-
-  fn init_config() -> &'static tauri_api::config::Config {
-    tauri_api::config::get().expect("unable to setup default config")
-  }
+  use std::{fs::read_to_string, path::Path};
 
   #[test]
   fn check_setup_content() {
-    let config = init_config();
-
     let tauri_dir = match option_env!("TAURI_DIR") {
       Some(d) => d.to_string(),
       None => env::current_dir()
@@ -298,7 +288,7 @@ mod test {
     #[cfg(embedded_server)]
     match res {
       Ok(Content::Url(u)) => assert!(u.contains("http://")),
-      _ => assert!(false),
+      _ => panic!("setup content failed"),
     }
 
     #[cfg(no_server)]
@@ -317,21 +307,24 @@ mod test {
           read_to_string(Path::new(&dist_dir).join("index.tauri.html")).unwrap()
         );
       }
-      _ => assert!(false),
+      _ => panic!("setup content failed"),
     }
 
     #[cfg(dev)]
-    match res {
-      Ok(Content::Url(dp)) => assert_eq!(dp, config.build.dev_path),
-      Ok(Content::Html(s)) => {
-        let dev_dir = &config.build.dev_path;
-        let dev_path = Path::new(dev_dir).join("index.tauri.html");
-        assert_eq!(
-          s,
-          read_to_string(dev_path).expect("failed to read dev path")
-        );
+    {
+      let config = tauri_api::config::get().expect("unable to setup default config");
+      match res {
+        Ok(Content::Url(dp)) => assert_eq!(dp, config.build.dev_path),
+        Ok(Content::Html(s)) => {
+          let dev_dir = &config.build.dev_path;
+          let dev_path = Path::new(dev_dir).join("index.tauri.html");
+          assert_eq!(
+            s,
+            read_to_string(dev_path).expect("failed to read dev path")
+          );
+        }
+        _ => panic!("setup content failed"),
       }
-      _ => assert!(false),
     }
   }
 
@@ -340,8 +333,8 @@ mod test {
   fn check_setup_port() {
     let res = super::setup_port();
     match res {
-      Ok((_s, _b)) => assert!(true),
-      _ => assert!(false),
+      Ok((_s, _b)) => {}
+      _ => panic!("setup port failed"),
     }
   }
 
@@ -356,7 +349,7 @@ mod test {
 
       match res {
         Ok(url) => assert!(url.contains(&p)),
-        Err(_) => assert!(false)
+        Err(e) => panic!("setup_server_url Err {:?}", e.to_string())
       }
     }
   }

+ 3 - 7
tauri/src/endpoints.rs

@@ -315,13 +315,13 @@ mod test {
       let res = super::init();
       match res {
         Ok(s) => assert_eq!(s, ""),
-        Err(_) => assert!(false),
+        Err(e) => panic!("init Err {:?}", e.to_string()),
       }
     } else if cfg!(event) {
       let res = super::init();
       match res {
         Ok(s) => assert!(s.contains("window.__TAURI__.promisified")),
-        Err(_) => assert!(false),
+        Err(e) => panic!("init Err {:?}", e.to_string()),
       }
     }
   }
@@ -331,11 +331,7 @@ mod test {
     #[cfg(event)]
     #[test]
     fn check_listen_fn(event in "", handler in "", once in proptest::bool::ANY) {
-      let res = super::event::listen_fn(event, handler, once);
-      match res {
-        Ok(_) => assert!(true),
-        Err(_) => assert!(false)
-      }
+      super::event::listen_fn(event, handler, once).expect("listen_fn failed");
     }
   }
 

+ 2 - 2
tauri/src/event.rs

@@ -142,9 +142,9 @@ mod test {
         // check to see if we get back a handler or not
         match handler {
           // pass on Some(handler)
-          Some(_) => assert!(true),
+          Some(_) => {},
           // Fail on None
-          None => assert!(false)
+          None => panic!("handler is None")
         }
       }
     }

+ 1 - 1
tauri/src/lib.rs

@@ -9,6 +9,7 @@
   all(not(debug_assertions), target_os = "windows"),
   windows_subsystem = "windows"
 )]
+//#![deny(clippy::all)]
 
 /// The asset management module.
 #[cfg(assets)]
@@ -145,7 +146,6 @@ mod test {
       // create dummy task function
       let dummy_task = move || {
         format!("{}-run-dummy-task", task);
-        assert!(true);
       };
       // call spawn
       crate::spawn(dummy_task);

+ 1 - 1
tauri/src/server.rs

@@ -10,7 +10,7 @@ pub fn asset_response(path: &str) -> Response<std::io::Cursor<Vec<u8>>> {
   );
   let asset = crate::assets::ASSETS
     .get(asset_path)
-    .expect(&format!("Could not read asset {}", asset_path))
+    .unwrap_or_else(|_| panic!("Could not read asset {}", asset_path))
     .into_owned();
   let mut response = Response::from_data(asset);
   let header;