Skip to main content

Merge pull request #52 from alexwlchan/add-tests

ID
a6d3ae5
date
2024-06-13 12:17:19+00:00
author
Alex Chan <alex@alexwlchan.net>
parents
8438e77, 86554cb
message
Merge pull request #52 from alexwlchan/add-tests

Add tests; fix a hyperlink issue
changed files
2 files, 23 additions, 4 deletions

Changed files

aws/_common.py (2460) → aws/_common.py (2519)

diff --git a/aws/_common.py b/aws/_common.py
index cc9305d..5ca83a8 100755
--- a/aws/_common.py
+++ b/aws/_common.py
@@ -1,6 +1,5 @@
-#!/usr/bin/env python3
-
 import functools
+import typing
 
 import boto3
 import hyperlink
@@ -73,8 +72,13 @@ def create_s3_session(s3_identifier, *, role_name="read_only"):
         return boto3.Session()
 
 
-def parse_s3_uri(s3_uri):
-    uri = hyperlink.URL.from_text(s3_uri)
+class S3Uri(typing.TypedDict):
+    Bucket: str
+    Path: str
+
+
+def parse_s3_uri(s3_uri: str) -> S3Uri:
+    uri = hyperlink.parse(s3_uri)
 
     if uri.scheme != "s3":
         raise ValueError(f"Unrecognised scheme in {s3_uri!r}, expected s3://")

aws/test_common.py (0) → aws/test_common.py (384)

diff --git a/aws/test_common.py b/aws/test_common.py
new file mode 100644
index 0000000..db2e93e
--- /dev/null
+++ b/aws/test_common.py
@@ -0,0 +1,15 @@
+import pytest
+
+from _common import parse_s3_uri
+
+
+def test_non_s3_uri_is_error():
+    with pytest.raises(ValueError, match="Unrecognised scheme"):
+        parse_s3_uri(s3_uri="https://www.example.com")
+
+
+def test_parses_s3_uri():
+    assert parse_s3_uri(s3_uri="s3://example-bukkit/my/text/file.txt") == {
+        "Bucket": "example-bukkit",
+        "Path": "my/text/file.txt",
+    }