Repository: urlstechie/urlchecker-python

Issue:
Remove "github/workspace" from filename in csv?
Not sure if this belongs to the library or action. Would it be possible (and make sense) to have the filename relative to the folder in which the URL checks are run?

Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58cb458..294cc8f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ and **Merged pull requests**. Critical items to know are:
 Referenced versions in headers are tagged on Github, in parentheses are for pypi.
 
 ## [vxx](https://github.com/urlstechie/urlschecker-python/tree/master) (master)
+ - csv save uses relative paths (0.0.19)
  - refactor check.py to be UrlChecker class, save with filename (0.0.18)
  - default for files needs to be empty string (not None) (0.0.17)
  - bug with incorrect return code on fail, add files flag (0.0.16)
diff --git a/urlchecker/core/check.py b/urlchecker/core/check.py
index f833023..17c01ce 100644
--- a/urlchecker/core/check.py
+++ b/urlchecker/core/check.py
@@ -9,6 +9,7 @@ For a copy, see <https://opensource.org/licenses/MIT>.
 
 import csv
 import os
+import re
 import sys
 from urlchecker.core import fileproc
 from urlchecker.core.urlproc import UrlCheckResult
@@ -72,7 +73,7 @@ class UrlChecker:
     def __repr__(self):
         return self.__str__()
 
-    def save_results(self, file_path, sep=",", header=None):
+    def save_results(self, file_path, sep=",", header=None, relative_paths=True):
         """
         Given a check_results dictionary, a dict with "failed" and "passed" keys (
         or more generally, keys to indicate some status), save a csv
@@ -84,6 +85,7 @@ class UrlChecker:
             - file_path (str): the file path (.csv) to save to.
             - sep (str): the separate to use (defaults to comma)
             - header (list): if not provided, will save URL,RESULT
+            - relative paths (bool) : save relative paths (default True)
 
         Returns:
             (str) file_path: a newly saved csv with the results
@@ -113,6 +115,14 @@ class UrlChecker:
 
             # Iterate through filenames, each with check results
             for file_name, result in self.checks.items():
+
+                # Derive the relative path based on self.path, or relative to run
+                if relative_paths:
+                    if self.path:
+                        file_name = re.sub(self.path, "", file_name).strip('/')
+                    else:
+                        file_name = os.path.relpath(file_name)
+
                 [writer.writerow([url, "passed", file_name]) for url in result.passed]
                 [writer.writerow([url, "failed", file_name]) for url in result.failed]
 
diff --git a/urlchecker/version.py b/urlchecker/version.py
index 72c9090..a046825 100644
--- a/urlchecker/version.py
+++ b/urlchecker/version.py
@@ -7,7 +7,7 @@ For a copy, see <https://opensource.org/licenses/MIT>.
 
 """
 
-__version__ = "0.0.18"
+__version__ = "0.0.19"
 AUTHOR = "Ayoub Malek, Vanessa Sochat"
 AUTHOR_EMAIL = "superkogito@gmail.com, vsochat@stanford.edu"
 NAME = "urlchecker"


Test patch:
diff --git a/tests/test_core_check.py b/tests/test_core_check.py
index 301020d..bd311cf 100644
--- a/tests/test_core_check.py
+++ b/tests/test_core_check.py
@@ -33,6 +33,7 @@ def test_check_files(file_paths, print_all, white_listed_urls, white_listed_patt
         file_paths,
         white_listed_urls=white_listed_urls,
         white_listed_patterns=white_listed_patterns,
+        retry_count = 1,
     )
 
 
@@ -59,6 +60,7 @@ def test_locally(local_folder_path, config_fname):
         file_paths=file_paths,
         white_listed_urls=white_listed_urls,
         white_listed_patterns=white_listed_patterns,
+        retry_count = 1,
     )
     print("Done.")
 
@@ -111,6 +113,20 @@ def test_check_run_save(tmp_path, retry_count):
     # Ensure content looks okay
     for line in lines[1:]:
         url, result, filename = line.split(",")
+
+        root = filename.split('/')[0]
         assert url.startswith("http")
         assert result in ["passed", "failed"]
         assert re.search("(.py|.md)$", filename)
+
+    # Save with full path
+    saved_file = checker.save_results(output_file, relative_paths=False)
+
+    # Read in output file
+    with open(saved_file, "r") as filey:
+        lines = filey.readlines()
+
+    # Ensure content looks okay
+    for line in lines[1:]:
+        url, result, filename = line.split(",")
+        assert not filename.startswith(root)
diff --git a/tests/test_core_urlproc.py b/tests/test_core_urlproc.py
index 6f6b4d9..f573cdc 100644
--- a/tests/test_core_urlproc.py
+++ b/tests/test_core_urlproc.py
@@ -25,24 +25,25 @@ def test_check_urls(file):
     assert not checker.passed
     assert not checker.failed
     assert not checker.all
+    assert not checker.white_listed
     checker.check_urls(urls)
 
     # Ensure we have the correct sums for passing/failing
-    assert len(checker.failed + checker.passed) == checker.count
+    assert len(checker.failed + checker.passed + checker.white_listed) == checker.count
     assert len(checker.all) == len(urls)
 
     # Ensure one not whitelisted is failed
-    assert "https://github.com/SuperKogito/URLs-checker/issues/1" in checker.failed
+    assert "https://github.com/SuperKogito/URLs-checker/issues/4" in checker.failed
 
     assert checker.print_all
 
     # Run again with whitelist of exact urls
     checker = UrlCheckResult(
-        white_listed_urls=["https://github.com/SuperKogito/URLs-checker/issues/1"]
+        white_listed_urls=["https://github.com/SuperKogito/URLs-checker/issues/4"]
     )
     checker.check_urls(urls)
     assert (
-        "https://github.com/SuperKogito/URLs-checker/issues/1" in checker.white_listed
+        "https://github.com/SuperKogito/URLs-checker/issues/4" in checker.white_listed
     )
 
     # Run again with whitelist of patterns
diff --git a/tests/test_core_whitelist.py b/tests/test_core_whitelist.py
index 25fe2fc..646b66a 100644
--- a/tests/test_core_whitelist.py
+++ b/tests/test_core_whitelist.py
@@ -12,7 +12,7 @@ def test_whitelisted():
     assert not white_listed(url)
 
     # Exact url provided as white list url
-    assert (white_listed(url), [url])
+    assert white_listed(url, [url])
 
     # Pattern provided as white list
-    assert (white_listed(url), [], ["https://white-listed/"])
+    assert white_listed(url, [], ["https://white-listed/"])
diff --git a/tests/test_files/sample_test_file.md b/tests/test_files/sample_test_file.md
index 565c562..d068e8c 100644
--- a/tests/test_files/sample_test_file.md
+++ b/tests/test_files/sample_test_file.md
@@ -5,27 +5,15 @@ The following is a list of test urls to extract.
  - [test url 3](https://github.com/SuperKogito/URLs-checker)
  - [test url 4](https://github.com/SuperKogito/URLs-checker/blob/master/README.md)
  - [test url 5](https://github.com/SuperKogito/URLs-checker/issues)
- - [test url 6](https://github.com/SuperKogito/URLs-checker/issues/1)
- - [test url 7](https://github.com/SuperKogito/URLs-checker/issues/2)
- - [test url 8](https://github.com/SuperKogito/URLs-checker/issues/3)
- - [test url 9](https://github.com/SuperKogito/URLs-checker/issues/4)
+ - [test url 6](https://github.com/SuperKogito/URLs-checker/issues/4)
 
-- [test url 10](https://github.com/SuperKogito/spafe/)
-- [test url 11](https://github.com/SuperKogito/spafe/issues)
-- [test url 12](https://github.com/SuperKogito/spafe/issues/1)
-- [test url 13](https://github.com/SuperKogito/spafe/issues/2)
-- [test url 14](https://github.com/SuperKogito/spafe/issues/3)
-- [test url 15](https://github.com/SuperKogito/spafe/issues/4)
-- [test url 16](https://github.com/SuperKogito/spafe/issues/5)
-- [test url 17](https://github.com/SuperKogito/spafe/issues/6)
-- [test url 18](https://github.com/SuperKogito/spafe/issues/7)
-- [test url 19](https://github.com/SuperKogito/spafe/issues/8)
+- [test url 7](https://github.com/SuperKogito/spafe/)
+- [test url 8](https://github.com/SuperKogito/spafe/issues)
+- [test url 9](https://github.com/SuperKogito/spafe/issues/1)
 
-- [test url 20](https://github.com/SuperKogito/Voice-based-gender-recognition)
-- [test url 21](https://github.com/SuperKogito/Voice-based-gender-recognition/issues)
-- [test url 22](https://github.com/SuperKogito/Voice-based-gender-recognition/issues/1)
+- [test url 10](https://github.com/SuperKogito/Voice-based-gender-recognition)
+- [test url 11](https://github.com/SuperKogito/Voice-based-gender-recognition/issues)
 - [test url 23](https://github.com/SuperKogito/Voice-based-gender-recognition/issues/2)
-- [test url 24](https://github.com/SuperKogito/Voice-based-gender-recognition/issues)
 
 - [broken_test url 1](https://none.html)
 - [broken_test url 2](https://github.com/SuperKogito/URLs-checker/README.md)