Repository: crate-ci/typos
Pull request #276 (2021-06-05 17:23:18)

fix(action): Be stricter/cleaner with entrypoint


Patch:
diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
index b3a5a9f..a5e0af4 100755
--- a/docker/entrypoint.sh
+++ b/docker/entrypoint.sh
@@ -1,59 +1,34 @@
 #!/bin/bash
 
-set -e
+set -eu
 
-printf "Found files in workspace:\n"
-ls
+log() {
+    echo -e "$1" >&2
+}
 
-printf "\nLooking for typos installed...\n"
-which typos
+CMD_NAME="typos"
+TARGET=${INPUT_FILES:-"."}
 
-COMMAND="typos"
-
-# Show the _typos.toml file
-if [ -f "_typos.toml" ]; then
-    echo "_typos.toml:"
-    cat _typos.toml
-    echo
+if [[ -z $(ls ${TARGET} 2>/dev/null) ]]; then
+    log "ERROR: Input files (${TARGET}) not found"
+    exit 1
 fi
-
-# Ignore implicit configuration files
-if [ "${INPUT_ISOLATED}" == "true" ]; then
-    COMMAND="${COMMAND} --isolated"
+if [[ -z $(which ${CMD_NAME} 2>/dev/null) ]]; then
+    log "ERROR: 'typos' not found"
+    exit 1
 fi
 
+COMMAND="${CMD_NAME} ${TARGET}"
 
-# Use a custom configuration file
-if [ ! -z "${INPUT_CONFIG}" ]; then
-
-    # It must exist
-    if [ ! -f "${INPUT_CONFIG}" ]; then
-        printf "${INPUT_CONFIG} does not exist.\n"
-        exit 1;
-    else
-        # Show the custom config to the user
-        printf "Custom config:\n"
-        cat "${INPUT_CONFIG}"    
-        echo
-    fi
-    COMMAND="${COMMAND} --config ${INPUT_CONFIG}"
+# Ignore implicit configuration files
+if [ "${INPUT_ISOLATED:-false}" == "true" ]; then
+    COMMAND+=" --isolated"
 fi
 
-# Files are technically optional
-if [ ! -z "${INPUT_FILES}" ]; then
-    COMMAND="${COMMAND} ${INPUT_FILES}"
+# Use a custom configuration file
+if [[ -n "${INPUT_CONFIG:-}" ]]; then
+    COMMAND+=" --config ${INPUT_CONFIG}"
 fi
 
-echo "Command: "
-echo "${COMMAND}"
-echo
-
+log "$ ${COMMAND}"
 ${COMMAND}
-retval=$?
-
-if [[ "${retval}" -eq 0 ]]; then
-   printf "No spelling mistakes found! 🎉️\n"
-else
-   printf "Spelling mistakes found! 😱️\n"
-   exit $retval;
-fi
diff --git a/src/bin/typos-cli/main.rs b/src/bin/typos-cli/main.rs
index d9dc667..8667b89 100644
--- a/src/bin/typos-cli/main.rs
+++ b/src/bin/typos-cli/main.rs
@@ -172,13 +172,14 @@ fn run_checks(
     let mut errors_found = false;
     for path in args.path.iter() {
         let cwd = if path == std::path::Path::new("-") {
-            global_cwd.as_path()
+            global_cwd.clone()
         } else if path.is_file() {
-            path.parent().unwrap()
+            let mut cwd = path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?;
+            cwd.pop();
+            cwd
         } else {
-            path.as_path()
+            path.clone()
         };
-        let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?;
 
         engine
             .init_dir(&cwd)


Test patch:
diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml
index 9a8a14a..74947f5 100644
--- a/.github/workflows/test-action.yml
+++ b/.github/workflows/test-action.yml
@@ -10,16 +10,16 @@ jobs:
       uses: actions/checkout@v2
 
     - name: Prepare file with mistakes.
-      run: echo "The quick brown foxx jumped over the slepy dog." > file.txt 
+      run: echo "Finallizes" > file.txt
     - name: Test force pass with mistakes
       continue-on-error: true
       uses: ./
-      with: 
+      with:
         files: ./file.txt
 
     - name: Prepare file with no mistakes.
-      run: echo "The quick brown fox jumped over the sleepy dog." > file.txt 
-    - name: Test force pass with no mistakes
+      run: echo "Finalizes" > file.txt
+    - name: Test pass with no mistakes
       uses: ./
-      with: 
+      with:
         files: ./file.txt
diff --git a/tests/cli.rs b/tests/cli.rs
index c65b771..9fcc0cd 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -22,3 +22,10 @@ fn test_stdin_correct() {
         .write_stdin("Apropriate world");
     cmd.assert().success().stdout("Appropriate world");
 }
+
+#[test]
+fn test_file_failure() {
+    let mut cmd = Command::cargo_bin("typos").unwrap();
+    cmd.arg("README.md");
+    cmd.assert().code(2);
+}