Qithub-BOT / QiiCipher

✅ GitHub の SSH 公開鍵でファイルを暗号化およびローカルの秘密鍵で復号・署名・検証するスクリプトのリポジトリです。
https://qiita.com/KEINOS/items/2abce1e5b15d799ac6d7
Creative Commons Attribution Share Alike 4.0 International
4 stars 3 forks source link

shfmt がチェック漏れしている #62

Open KEINOS opened 3 years ago

KEINOS commented 3 years ago

TL; DR (今北産業)

  1. 6d0cf1e 現在の ./.github/run-lint.shshfmt の lint チェック時に検知漏れがある。
  2. どうも shfmt のバージョンにも関係していそう
    • shfmt v3.3.0 -> 検知しない
    • shfmt v3.2.1-0 -> 検知しない
    • shfmt v3.1.1-0 -> 検知する
  3. shfmt のバージョンによって .editorconfig が反映されないケースがあるっぽい

TS; DR

以下の 40 行目のインデントにスペースが 1 つ余計に入っています。

https://github.com/Qithub-BOT/QiiCipher/blob/6d0cf1e7bf149e47968bf5a1901c4e87d9f7ad22/.github/run-test.sh#L31-L41

しかし、テスト結果はパスしています。

===============================================================================
 Requirement Check for Linting and Static Analysis
===============================================================================
- ShellCheck version: 0.7.0
- shfmt version: v3.3.0
-------------------------------------------------------------------------------
 Running linters
-------------------------------------------------------------------------------
- Shellformat ... OK
- ShellCheck ... OK

これは少し古いローカルの shfmt v3.2.1-0 でも同じ現象でした。

shfmt -i=4 ./.github/run-tests.shインデントをオプションで指定すると検知することから、.editorconfig が反映されないパターンがあるようです。

$ shfmt --version
3.2.1-0
$ shfmt -d ./.github/run-test.sh
$ echo $?
0

$ shfmt -i=4 -d ./.github/run-test.sh
--- ./.github/run-test.sh.orig
+++ ./.github/run-test.sh
@@ -37,7 +37,7 @@
         return $FAILURE
     }

-     echo "$(echo "$result" | head -n 2 | tail -n 1)" 'OK'
+    echo "$(echo "$result" | head -n 2 | tail -n 1)" 'OK'
 }

 # -----------------------------------------------------------------------------

問題は、さらに古い shfmt のバージョンだと検知することです。shellspec の Alpine Docker イメージshfmt を入れると shfmt v3.1.1-0 が入るのですが、その場合は検知します。

#14 0.278 ===============================================================================                                                                                                                                         
#14 0.278  Requirement Check for Linting and Static Analysis
#14 0.278 ===============================================================================
#14 0.283 - ShellCheck version: 0.7.1
#14 0.289 - shfmt version: 3.1.1-0
#14 0.290 -------------------------------------------------------------------------------
#14 0.290  Running linters
#14 0.290 -------------------------------------------------------------------------------
#14 0.290 - Shellformat ... --- ./.github/run-test.sh.orig
#14 0.305 +++ ./.github/run-test.sh
#14 0.305 @@ -1,71 +1,71 @@
#14 0.305  #!/bin/sh
#14 0.305  # =============================================================================
#14 0.305  #  ShellSpec による動的/単体テストの実行スクリプト
#14 0.305  # =============================================================================
#14 0.305  
...(略)...
#14 0.306  # -----------------------------------------------------------------------------
#14 0.306  #  Functions
#14 0.306  # -----------------------------------------------------------------------------
#14 0.306  
#14 0.306  runShellSpec() {
#14 0.307      printf "%s" '- ShellSpec '
#14 0.307  
#14 0.307      result=$(shellspec 2>&1) || {
#14 0.307          printf >&2 ": NG\n%s" "$result"
#14 0.307  
#14 0.307          return $FAILURE
#14 0.307      }
#14 0.307  
#14 0.307 -     echo "$(echo "$result" | head -n 2 | tail -n 1)" 'OK'
#14 0.307 +    echo  "$(echo "$result" | head -n 2 | tail -n 1)" 'OK'
#14 0.307  }
...(略)...

ただ、このバージョン(shfmt v3.1.1-0)は shellspec のテスト構文を正しく解釈できないため、とんでもない数のエラーを吐き出します。そのため、shfmt v3.2 以降での対策が必要です。

FROM shellspec/shellspec:latest AS testbuild

# Install miminum requirements for QiiCipher
RUN apk add --no-cache \
    openssl \
    openssh \
    ca-certificates && update-ca-certificates

# Install requirements for testing
RUN apk add --no-cache \
    git \
    shellcheck \
    shfmt

# Copy the hole repo
COPY . /app
WORKDIR /app

# Run tests
RUN \
    /app/.github/run-lint.sh \
    && /app/.github/run-test.sh
KEINOS commented 3 years ago

どうも、shfmt 本家の issue #393 やソースを見ると、shfmt の呼び出し時にオプションがある場合は .editorconfig は無視されるっぽい。

以下で -d オプションを付けてるのが原因っぽい(未検証)

https://github.com/Qithub-BOT/QiiCipher/blob/6d0cf1e7bf149e47968bf5a1901c4e87d9f7ad22/.github/run-lint.sh#L57-L81