Skip to content

Commit

Permalink
Merge pull request #13 from Mr-Sunglasses/fix/tests
Browse files Browse the repository at this point in the history
Fix/tests
  • Loading branch information
Mr-Sunglasses authored Apr 6, 2024
2 parents 89bbdb8 + 5863bb8 commit 24c96f1
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 35 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test paste.py 🐍

on:
pull_request:
branches: [ master ]


jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Create config mock
run: mkdir ~/.ssh && touch ~/.ssh/config
- name: Install dependencies
run: pip3 install . && pip3 install pytest
- name: Run Test
run: pytest
97 changes: 62 additions & 35 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
import os
import re
from bitssh.utils import get_config_path, get_config_content


def test_get_config_path():
config_path = get_config_path()
assert isinstance(config_path, str)
assert os.path.isfile(config_path)


def test_get_config_content():
test_config_file = "config"
# Create a test config file with sample SSH server configurations
with open(test_config_file, 'w') as f:
f.write("""
Host test1
Hostname example.com
User user1
port 22
Host test2
Hostname example.org
User user2
port 22
""")

config_content = get_config_content(config_file=test_config_file)
assert isinstance(config_content, dict)
assert 'test1' in config_content
assert 'test2' in config_content
assert config_content['test1']['User'] == 'user1'
assert config_content['test2']['Hostname'] == 'example.org'

# Clean up the test config file
os.remove(test_config_file)
import unittest
from unittest.mock import patch
from bitssh.utils import ConfigPathUtility


class TestConfigPathUtility(unittest.TestCase):
def setUp(self):
self.mock_config_data = """
Host testHost1
HostName test.hostname1.com
User testUser1
Host testHost2
HostName test.hostname2.com
User testUser2
"""

def tearDown(self):
if os.path.exists("config"):
os.remove("config")

@patch("os.path.exists", return_value=True)
@patch("builtins.open")
def test_get_config_content_success(self, mock_open, mock_exists):
mock_open.return_value.__enter__.return_value.read.return_value = (
self.mock_config_data
)
expected = {
"testHost1": {"Hostname": "test.hostname1.com", "User": "testUser1"},
"testHost2": {"Hostname": "test.hostname2.com", "User": "testUser2"},
}
result = ConfigPathUtility.get_config_content()
self.assertEqual(result, expected)

@patch("os.path.exists", return_value=True)
@patch("builtins.open")
def test_get_config_file_row_data(self, mock_open, mock_exists):
mock_open.return_value.__enter__.return_value.read.return_value = (
self.mock_config_data
)
expected_rows = [
("test.hostname1.com", "testHost1", "22", "testUser1"),
("test.hostname2.com", "testHost2", "22", "testUser2"),
]
rows = ConfigPathUtility.get_config_file_row_data()
self.assertEqual(rows, expected_rows)

@patch("os.path.exists", return_value=True)
@patch("builtins.open")
def test_get_config_file_host_data(self, mock_open, mock_exists):
mock_open.return_value.__enter__.return_value.read.return_value = (
self.mock_config_data
)
expected_hosts = [
"🖥️ -> testHost1",
"🖥️ -> testHost2",
]
hosts = ConfigPathUtility.get_config_file_host_data()
self.assertEqual(hosts, expected_hosts)


if __name__ == "__main__":
unittest.main()

0 comments on commit 24c96f1

Please sign in to comment.