summaryrefslogtreecommitdiff
path: root/servers/gitlab_glab/tests/test_server.py
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-05-28 15:58:05 +0200
committerDawid Rycerz <dawid@rycerz.xyz>2025-05-28 15:58:05 +0200
commit126ce542dca21e62b38049f67c7f4c951284a2c2 (patch)
tree467d91d26bb7a29bb0ddc4ab3149e8c2d99e77f6 /servers/gitlab_glab/tests/test_server.py
parent54b521f19e00bea52304f7379ca59de0c2e20962 (diff)
feat(gitlab_glab): add diff content filtering to get_mr_diff
- Add filter_diff_content method to filter out unwanted file extensions - Default filtering excludes .lock and .log files to reduce noise - Smart lock file detection handles package-lock.json, yarn.lock, etc. - Customizable filtering via filter_extensions parameter - Filtering can be disabled by passing empty list - Add comprehensive tests for all filtering scenarios - Update README.md with filtering documentation - Maintain backward compatibility with existing API
Diffstat (limited to 'servers/gitlab_glab/tests/test_server.py')
-rw-r--r--servers/gitlab_glab/tests/test_server.py287
1 files changed, 287 insertions, 0 deletions
diff --git a/servers/gitlab_glab/tests/test_server.py b/servers/gitlab_glab/tests/test_server.py
index 867b2c0..13f90af 100644
--- a/servers/gitlab_glab/tests/test_server.py
+++ b/servers/gitlab_glab/tests/test_server.py
@@ -1020,3 +1020,290 @@ index 1234567..abcdefg 100644
["ci", "run"],
working_dir,
)
+
+ @patch.object(GitLabServer, "execute_glab_command")
+ def test_get_mr_diff_with_default_filtering(self, mock_execute: MagicMock) -> None:
+ """Test MR diff with default filtering (.lock and .log files)."""
+ # Mock diff content with .lock and .log files
+ diff_content = """diff --git a/package-lock.json b/package-lock.json
+index 1234567..abcdefg 100644
+--- a/package-lock.json
++++ b/package-lock.json
+@@ -1,3 +1,4 @@
+ {
+ "name": "test",
++ "version": "1.0.0",
+ "lockfileVersion": 1
+}
+diff --git a/debug.log b/debug.log
+index 1234567..abcdefg 100644
+--- a/debug.log
++++ b/debug.log
+@@ -1,2 +1,3 @@
+ [INFO] Starting application
++[INFO] Loading configuration
+ [INFO] Application ready
+diff --git a/src/main.py b/src/main.py
+index 1234567..abcdefg 100644
+--- a/src/main.py
++++ b/src/main.py
+@@ -1,3 +1,4 @@
+ def main():
+ print("Hello")
++ print("World")
+ return 0"""
+ mock_execute.return_value = (True, diff_content)
+
+ server = GitLabServer()
+ working_dir = "/test/directory"
+ result = server.get_mr_diff(working_directory=working_dir)
+
+ # Check that .lock and .log files are filtered out
+ filtered_diff = result["diff"]
+ assert "package-lock.json" not in filtered_diff
+ assert "debug.log" not in filtered_diff
+ assert "src/main.py" in filtered_diff
+ assert 'print("World")' in filtered_diff
+
+ @patch.object(GitLabServer, "execute_glab_command")
+ def test_get_mr_diff_with_custom_filtering(self, mock_execute: MagicMock) -> None:
+ """Test MR diff with custom filtering extensions."""
+ # Mock diff content with various file types
+ diff_content = """diff --git a/test.txt b/test.txt
+index 1234567..abcdefg 100644
+--- a/test.txt
++++ b/test.txt
+@@ -1,2 +1,3 @@
+ line 1
++new line
+ line 2
+diff --git a/config.json b/config.json
+index 1234567..abcdefg 100644
+--- a/config.json
++++ b/config.json
+@@ -1,3 +1,4 @@
+ {
+ "setting": "value"
++ "new_setting": "new_value"
+ }
+diff --git a/temp.tmp b/temp.tmp
+index 1234567..abcdefg 100644
+--- a/temp.tmp
++++ b/temp.tmp
+@@ -1,2 +1,3 @@
+ temp data
++more temp data
+ end"""
+ mock_execute.return_value = (True, diff_content)
+
+ server = GitLabServer()
+ working_dir = "/test/directory"
+ result = server.get_mr_diff(
+ working_directory=working_dir,
+ filter_extensions=[".tmp", ".json"]
+ )
+
+ # Check that .tmp and .json files are filtered out
+ filtered_diff = result["diff"]
+ assert "temp.tmp" not in filtered_diff
+ assert "config.json" not in filtered_diff
+ assert "test.txt" in filtered_diff
+ assert "new line" in filtered_diff
+
+ @patch.object(GitLabServer, "execute_glab_command")
+ def test_get_mr_diff_with_no_filtering(self, mock_execute: MagicMock) -> None:
+ """Test MR diff with filtering disabled (empty list)."""
+ # Mock diff content with .lock file
+ diff_content = """diff --git a/package-lock.json b/package-lock.json
+index 1234567..abcdefg 100644
+--- a/package-lock.json
++++ b/package-lock.json
+@@ -1,3 +1,4 @@
+ {
+ "name": "test",
++ "version": "1.0.0",
+ "lockfileVersion": 1
+}"""
+ mock_execute.return_value = (True, diff_content)
+
+ server = GitLabServer()
+ working_dir = "/test/directory"
+ result = server.get_mr_diff(
+ working_directory=working_dir,
+ filter_extensions=[]
+ )
+
+ # Check that no filtering occurred
+ filtered_diff = result["diff"]
+ assert "package-lock.json" in filtered_diff
+ assert '"version": "1.0.0"' in filtered_diff
+
+ def test_filter_diff_content_basic(self) -> None:
+ """Test basic diff content filtering."""
+ server = GitLabServer()
+
+ diff_content = """diff --git a/src/main.py b/src/main.py
+index 1234567..abcdefg 100644
+--- a/src/main.py
++++ b/src/main.py
+@@ -1,3 +1,4 @@
+ def main():
+ print("Hello")
++ print("World")
+ return 0
+diff --git a/package-lock.json b/package-lock.json
+index 1234567..abcdefg 100644
+--- a/package-lock.json
++++ b/package-lock.json
+@@ -1,3 +1,4 @@
+ {
+ "name": "test",
++ "version": "1.0.0",
+ "lockfileVersion": 1
+}"""
+
+ filtered = server.filter_diff_content(diff_content, [".lock"])
+
+ assert "src/main.py" in filtered
+ assert 'print("World")' in filtered
+ assert "package-lock.json" not in filtered
+ assert '"version": "1.0.0"' not in filtered
+
+ def test_filter_diff_content_multiple_extensions(self) -> None:
+ """Test filtering with multiple extensions."""
+ server = GitLabServer()
+
+ diff_content = """diff --git a/app.py b/app.py
+index 1234567..abcdefg 100644
+--- a/app.py
++++ b/app.py
+@@ -1,2 +1,3 @@
+ import logging
++logging.basicConfig()
+ app = Flask(__name__)
+diff --git a/debug.log b/debug.log
+index 1234567..abcdefg 100644
+--- a/debug.log
++++ b/debug.log
+@@ -1,2 +1,3 @@
+ [INFO] Starting
++[DEBUG] Debug message
+ [INFO] Ready
+diff --git a/yarn.lock b/yarn.lock
+index 1234567..abcdefg 100644
+--- a/yarn.lock
++++ b/yarn.lock
+@@ -1,3 +1,4 @@
+ # THIS IS AN AUTOGENERATED FILE
++# Dependencies
+ package@1.0.0:
+ version "1.0.0"""
+
+ filtered = server.filter_diff_content(diff_content, [".log", ".lock"])
+
+ assert "app.py" in filtered
+ assert "logging.basicConfig()" in filtered
+ assert "debug.log" not in filtered
+ assert "yarn.lock" not in filtered
+ assert "[DEBUG] Debug message" not in filtered
+ assert "# Dependencies" not in filtered
+
+ def test_filter_diff_content_empty_extensions(self) -> None:
+ """Test filtering with empty extensions list."""
+ server = GitLabServer()
+
+ diff_content = """diff --git a/package-lock.json b/package-lock.json
+index 1234567..abcdefg 100644
+--- a/package-lock.json
++++ b/package-lock.json
+@@ -1,2 +1,3 @@
+ {
++ "name": "test",
+ "lockfileVersion": 1
+}"""
+
+ filtered = server.filter_diff_content(diff_content, [])
+
+ # Should return original content when no extensions to filter
+ assert filtered == diff_content
+
+ def test_filter_diff_content_malformed_diff_header(self) -> None:
+ """Test filtering with malformed diff headers."""
+ server = GitLabServer()
+
+ diff_content = """diff --git a/file.py
+index 1234567..abcdefg 100644
+--- a/file.py
++++ b/file.py
+@@ -1,2 +1,3 @@
+ line 1
++new line
+ line 2
+diff --git a/package-lock.json b/package-lock.json
+index 1234567..abcdefg 100644
+--- a/package-lock.json
++++ b/package-lock.json
+@@ -1,2 +1,3 @@
+ {
++ "version": "1.0.0",
+ "lockfileVersion": 1
+}"""
+
+ filtered = server.filter_diff_content(diff_content, [".lock"])
+
+ # Malformed header should not be filtered
+ assert "diff --git a/file.py" in filtered
+ assert "new line" in filtered
+ # Properly formed header with .lock extension should be filtered
+ assert "package-lock.json" not in filtered
+ assert '"version": "1.0.0"' not in filtered
+
+ @patch.object(GitLabServer, "execute_glab_command")
+ def test_get_mr_diff_filtering_with_large_file(self, mock_execute: MagicMock) -> None:
+ """Test that filtering works correctly even when creating temp files."""
+ # Create a large diff that will still be large after filtering
+ large_diff_base = """diff --git a/src/main.py b/src/main.py
+index 1234567..abcdefg 100644
+--- a/src/main.py
++++ b/src/main.py
+@@ -1,3 +1,4 @@
+ def main():
+ print("Hello")
++ print("World")
+ return 0
+"""
+ # Add a lot of content to main.py to make it large
+ large_diff = large_diff_base + "+ # " + "Large comment line\n" * 3000
+
+ # Add package-lock.json content that should be filtered out
+ large_diff += """diff --git a/package-lock.json b/package-lock.json
+index 1234567..abcdefg 100644
+--- a/package-lock.json
++++ b/package-lock.json
+@@ -1,500 +1,501 @@
+ {
+ "name": "test",
++ "version": "1.0.0",
+""" + " \"dependency\": \"^1.0.0\",\n" * 1000 + "}\n"
+
+ mock_execute.return_value = (True, large_diff)
+
+ server = GitLabServer()
+ working_dir = "/test/directory"
+
+ with patch("tempfile.NamedTemporaryFile") as mock_temp:
+ mock_file = MagicMock()
+ mock_file.name = "/tmp/test_diff.diff"
+ mock_temp.return_value.__enter__.return_value = mock_file
+
+ result = server.get_mr_diff(
+ working_directory=working_dir,
+ max_size_kb=1 # Force temp file creation
+ )
+
+ # Should still filter before saving to temp file
+ assert result["diff_too_large"] is True
+ # The filtered content should be written to temp file
+ written_content = mock_file.write.call_args[0][0]
+ assert "src/main.py" in written_content
+ assert "package-lock.json" not in written_content