emacs/var/elfeed/db/data/53/53faa5c85858b6fdd5024d57f88b04fc2101580d
2022-01-03 12:49:32 -06:00

69 lines
5.9 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<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 Ill 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 Ive 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>Theres nothing fancy here. Were 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 youre 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 its super fast.</li>
<li>Its 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 wont be surprised if someone actually has created a public reusable workflow for spell-checking code already.</li>
</ul>
<p>Thats 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. Id 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>