Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Fix the Codacy findings of the remote cache

- docs/remote-cache.md names Basic and Bearer without spaces inside code
  spans (MD038).
- DecodeOutput has no empty default clause (S3532).
- RemoteCacheException is public, like ProjectException (S3871).
- The fake cache of the tests answers GET and HEAD, and PUT, in methods of
  their own (CCN 12).
Commit
36f96c1440ab54710a56833b4394bb6cb897aba9
Author
Leitwolf11 ·
Parents
4ed05c9d5a5c

Files · Download .tar.gz

Changes

 docs/remote-cache.md                              |  6 +--
 src/Progmasoft.Tazel/Cache/ActionResultMessage.cs |  2 -
 src/Progmasoft.Tazel/Cache/RemoteCache.cs         |  2 +-
 tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs  | 52 ++++++++++++++---------
 4 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/docs/remote-cache.md b/docs/remote-cache.md
index ee100cc..eace3ec 100644
--- a/docs/remote-cache.md
+++ b/docs/remote-cache.md
@@ -20,9 +20,9 @@ cache = Cache.Define(new() {
 ```
 
 - The URL is `https`; `http` only for a cache on this machine (`localhost`, `127.0.0.1`, `[::1]`).
-- `Token` goes in the `Authorization` header. One that starts with `Basic ` or `Bearer ` is sent as it is, so a cache
-  behind HTTP basic authentication takes `Basic ` and the base64 of `user:password`; any other token is sent as
-  `Bearer <token>`.
+- `Token` goes in the `Authorization` header. One that starts with the word `Basic` or `Bearer` and a space is sent as
+  it is, so a cache behind HTTP basic authentication takes `Basic`, a space, and the base64 of `user:password`; any
+  other token is sent after `Bearer` and a space.
 - `Read` takes results from the cache before an action runs; `Write` gives it the results of actions that ran here.
   A machine that only reads, such as a pull request build, never writes.
 
diff --git a/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs b/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs
index 9242e2c..40a8c33 100644
--- a/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs
+++ b/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs
@@ -97,8 +97,6 @@ internal static class ActionResultMessage
                     break;
                 case { Wire: -1 }:
                     return null;
-                default:
-                    break;
             }
         }
 
diff --git a/src/Progmasoft.Tazel/Cache/RemoteCache.cs b/src/Progmasoft.Tazel/Cache/RemoteCache.cs
index 5702619..6072606 100644
--- a/src/Progmasoft.Tazel/Cache/RemoteCache.cs
+++ b/src/Progmasoft.Tazel/Cache/RemoteCache.cs
@@ -138,4 +138,4 @@ internal sealed class RemoteCache : IDisposable
 }
 
 /// <summary>A remote cache answered with an error or could not be reached.</summary>
-internal sealed class RemoteCacheException(string message) : Exception(message);
+public sealed class RemoteCacheException(string message) : Exception(message);
diff --git a/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs b/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs
index 962937d..118c71b 100644
--- a/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs
@@ -295,32 +295,44 @@ internal sealed class FakeRemoteCache : IDisposable
         var key = parts[1];
         switch (context.Request.HttpMethod)
         {
-            case "GET" or "HEAD" when store.TryGetValue(key, out var bytes):
-                context.Response.StatusCode = 200;
-                context.Response.ContentLength64 = bytes.Length;
-                if (context.Request.HttpMethod == "GET")
-                {
-                    await context.Response.OutputStream.WriteAsync(bytes);
-                }
-
+            case "GET" or "HEAD":
+                await GetAsync(context, store, key);
                 break;
             case "PUT":
-                using (var body = new MemoryStream())
-                {
-                    await context.Request.InputStream.CopyToAsync(body);
-                    var bytes = body.ToArray();
-                    var valid = store == Actions ? ActionResultMessage.Decode(bytes) is not null : Digests.Sha256(bytes) == key;
-                    context.Response.StatusCode = valid ? 200 : 400;
-                    if (valid)
-                    {
-                        store[key] = bytes;
-                    }
-                }
-
+                await PutAsync(context, store, key);
                 break;
             default:
                 context.Response.StatusCode = 404;
                 break;
         }
     }
+
+    private static async Task GetAsync(HttpListenerContext context, ConcurrentDictionary<string, byte[]> store, string key)
+    {
+        if (!store.TryGetValue(key, out var bytes))
+        {
+            context.Response.StatusCode = 404;
+            return;
+        }
+
+        context.Response.StatusCode = 200;
+        context.Response.ContentLength64 = bytes.Length;
+        if (context.Request.HttpMethod == "GET")
+        {
+            await context.Response.OutputStream.WriteAsync(bytes);
+        }
+    }
+
+    private async Task PutAsync(HttpListenerContext context, ConcurrentDictionary<string, byte[]> store, string key)
+    {
+        using var body = new MemoryStream();
+        await context.Request.InputStream.CopyToAsync(body);
+        var bytes = body.ToArray();
+        var valid = store == Actions ? ActionResultMessage.Decode(bytes) is not null : Digests.Sha256(bytes) == key;
+        context.Response.StatusCode = valid ? 200 : 400;
+        if (valid)
+        {
+            store[key] = bytes;
+        }
+    }
 }