69 lines
5.9 KiB
Plaintext
69 lines
5.9 KiB
Plaintext
<p>A couple of years ago I wrote an article about <a href="https://metaredux.com/posts/2019/05/24/eradicate-typos-in-source-code.html">dealing with typos in your source code</a>. Today I’ll follow up
|
||
with a simple recipe to automate this spell-checking process using GitHub Actions (GHA).<sup id="fnref:1"><a class="footnote" href="https://metaredux.com/posts/2021/11/26/automate-spell-checking-in-your-projects-with-github-actions.html#fn:1" rel="footnote">1</a></sup></p>
|
||
|
||
<p>Lately I’ve been adding the following <a href="https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions">GHA workflow</a> to all of my OSS projects:</p>
|
||
|
||
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">name</span><span class="pi">:</span> <span class="s">Spell Checking</span>
|
||
|
||
<span class="na">on</span><span class="pi">:</span> <span class="pi">[</span><span class="nv">pull_request</span><span class="pi">]</span>
|
||
|
||
<span class="na">jobs</span><span class="pi">:</span>
|
||
<span class="na">codespell</span><span class="pi">:</span>
|
||
<span class="na">name</span><span class="pi">:</span> <span class="s">Check spelling with codespell</span>
|
||
<span class="na">runs-on</span><span class="pi">:</span> <span class="s">ubuntu-latest</span>
|
||
<span class="na">strategy</span><span class="pi">:</span>
|
||
<span class="na">matrix</span><span class="pi">:</span>
|
||
<span class="na">python-version</span><span class="pi">:</span> <span class="pi">[</span><span class="nv">3.8</span><span class="pi">]</span>
|
||
<span class="na">steps</span><span class="pi">:</span>
|
||
<span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v2</span>
|
||
<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Set up Python $</span>
|
||
<span class="na">uses</span><span class="pi">:</span> <span class="s">actions/setup-python@v2</span>
|
||
<span class="na">with</span><span class="pi">:</span>
|
||
<span class="na">python-version</span><span class="pi">:</span> <span class="s">$</span>
|
||
<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Install dependencies</span>
|
||
<span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
|
||
<span class="s">python -m pip install --upgrade pip</span>
|
||
<span class="s">pip install codespell</span>
|
||
<span class="s">if [ -f requirements.txt ]; then pip install -r requirements.txt; fi</span>
|
||
<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Check spelling with codespell</span>
|
||
<span class="na">run</span><span class="pi">:</span> <span class="s">codespell --ignore-words=codespell.txt || exit </span><span class="m">1</span>
|
||
<span class="na">misspell</span><span class="pi">:</span>
|
||
<span class="na">name</span><span class="pi">:</span> <span class="s">Check spelling with misspell</span>
|
||
<span class="na">runs-on</span><span class="pi">:</span> <span class="s">ubuntu-latest</span>
|
||
<span class="na">steps</span><span class="pi">:</span>
|
||
<span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v2</span>
|
||
<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Install</span>
|
||
<span class="na">run</span><span class="pi">:</span> <span class="s">wget -O - -q https://git.io/misspell | sh -s -- -b .</span>
|
||
<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Misspell</span>
|
||
<span class="na">run</span><span class="pi">:</span> <span class="s">./misspell -error</span>
|
||
</code></pre></div></div>
|
||
|
||
<p>There’s nothing fancy here. We’re just installing a couple of popular spell-checkers for code and we run them
|
||
on everything within the Git repository. If they discover any problems the build will fail.</p>
|
||
|
||
<p>Just save the code above under <code class="language-plaintext highlighter-rouge">.github/workflows/spell_checking.yml</code> and you’re good to go.
|
||
The workflow will get triggered for each subsequent pull request.</p>
|
||
|
||
<p>A couple of things to note:</p>
|
||
|
||
<ul>
|
||
<li>This workflow uses both <a href="https://github.com/codespell-project/codespell">codespell</a> and <a href="https://github.com/client9/misspell">misspell</a>. Using both might be an overkill for some of you. Personally, I like <code class="language-plaintext highlighter-rouge">misspell</code> more, as it’s super fast.</li>
|
||
<li>It’s a good idea to run both tools locally first and address any existing typos.</li>
|
||
<li>You may want to limit the checks only to files that were changed in the pull request.</li>
|
||
<li>You may want to specify a locale for <code class="language-plaintext highlighter-rouge">misspell</code> if you want to enforce a specific flavor of English (e.g. <code class="language-plaintext highlighter-rouge">misspell -locale US</code>).</li>
|
||
<li>This should probably be made a <a href="https://docs.github.com/en/actions/learn-github-actions/reusing-workflows">reusable workflow</a>. I won’t be surprised if someone actually has created a public reusable workflow for spell-checking code already.</li>
|
||
</ul>
|
||
|
||
<p>That’s all I have for you today. Big thanks to my fellow OSS hacker <a href="https://github.com/koic">Koichi
|
||
Ito</a>, who came up with the idea of making
|
||
spell-checking a CI step. In hindsight it seems like a very obvious thing to do,
|
||
but it was always an afterthought for me. I’d be curious to hear how others are
|
||
dealing with typos in their codebases.</p>
|
||
|
||
<div class="footnotes">
|
||
<ol>
|
||
<li id="fn:1">
|
||
<p>The outlined approach can easily be adapted for any other CI. <a class="reversefootnote" href="https://metaredux.com/posts/2021/11/26/automate-spell-checking-in-your-projects-with-github-actions.html#fnref:1">↩</a></p>
|
||
</li>
|
||
</ol>
|
||
</div> |