-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Short article about a misunderstanding I had with fstest.mapfs that
broke my tests.
- Loading branch information
1 parent
f67a44e
commit 31cb2d1
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<!DOCTYPE html> | ||
|
||
<head> | ||
<script type="text/javascript" src="/main.js"></script> | ||
<title>fstest mapfs file does not exist error</title> | ||
<meta name="andrew-published-at" content="2024-03-27" </head> | ||
|
||
<body> | ||
<nav class="navigation"> | ||
<a href="/" class="link">front page</a> | ||
<a href="/articles/index.html" class="link">articles</a> | ||
<a href="/projects/index.html" class="link">projects</a> | ||
</nav> | ||
|
||
<link rel="stylesheet" href="/styles.css"> | ||
<div id="header"></div> | ||
<main> | ||
<article> | ||
<section id="introduction"> | ||
<p> | ||
In my tests, I defined an <code>fstest.MapFS</code> like this | ||
<code> | ||
<pre> | ||
contentRoot := fstest.MapFS{ | ||
"/parentDir/index.html": &fstest.MapFile{Data: []byte("content"), Mode: 0o755}, | ||
"/parentDir/childDir/1-2-3.html": &fstest.MapFile{Data: []byte("other content"`), Mode: 0o755}, | ||
} | ||
</pre> | ||
</code> | ||
Simple, and yet when I ran a test against it, I received this error: | ||
<code> | ||
<pre> | ||
2024/03/27 19:42:59 http: panic serving [::1]:57135: open parentDir: file does not exist | ||
</pre> | ||
</code> | ||
|
||
</p> | ||
</section> | ||
|
||
<section id="resolution"> | ||
<h1>What's the misunderstanding?</h1> | ||
<p> | ||
This one took me a couple of hours to finally understand. | ||
</p> | ||
<p> | ||
An <code>fstest.MapFS</code> is pretty much a hash map. <i>If you don't have a precise match for the key in the map | ||
then you get a <code>file does not exist</code> error.</i> Here's the implementation that shows that: | ||
<code> | ||
<pre> | ||
file := fsys[name] | ||
</pre> | ||
- reference in <a href="https://cs.opensource.google/go/go/+/refs/tags/go1.22.1:src/testing/fstest/mapfs.go;l=51">the go source code</a>. | ||
</code> | ||
</p> | ||
<h1>What's the fix?</h1> | ||
<p> | ||
|
||
In my case, I was performing some path munging in my logic so that I was searching for paths relatively, not absolutely | ||
using a leading "/", so my search string was "parentDir/index.html". | ||
</p> | ||
<p> | ||
Once I understood that <i>the path in the <code>fstest.MapFS</code> does not need to be an absolute path</i>, because <i>it is only | ||
a key in a hash map</i>, I redefined my structure like this and got my tests passing: | ||
<code> | ||
<pre> | ||
contentRoot := fstest.MapFS{ | ||
"/parentDir/index.html": &fstest.MapFile{Data: []byte("content"), Mode: 0o755}, | ||
"/parentDir/childDir/1-2-3.html": &fstest.MapFile{Data: []byte("other content"`), Mode: 0o755}, | ||
} | ||
</pre> | ||
</code> | ||
</p> | ||
</section> | ||
|
||
</article> | ||
</main> | ||
</body> | ||
|