Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ohtaman committed Dec 2, 2024
1 parent 85c938f commit 271f05e
Showing 1 changed file with 4 additions and 62 deletions.
66 changes: 4 additions & 62 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def setUp(self):
f.write(
"import os\n"
"import sys\n"
"from streamlit import st\n"
"from streamlit import web\n"
"from streamlit.web import cli\n"
"import pandas as pd\n"
)

Expand All @@ -35,7 +36,7 @@ def tearDown(self):
def test_extract_imports(self):
# Test extracting imports from a script
imports = extract_imports(self.script_path)
self.assertSetEqual(set(imports), {"os", "sys", "streamlit", "pandas"})
self.assertSetEqual(set(imports), {"os", "sys", "streamlit.web", "streamlit.web.cli", "pandas"})

def test_parse_streamlit_options_from_list(self):
# Test parsing Streamlit options from a list
Expand All @@ -56,13 +57,11 @@ def test_parse_streamlit_options_empty(self):

@patch("streamlit_desktop_app.build.PyInstaller.__main__.run")
@patch("streamlit_desktop_app.build.extract_imports", return_value=["os", "sys", "streamlit", "pandas"])
def test_build_executable_raw(self, mock_extract_imports, mock_pyinstaller_run):
def test_build_executable(self, mock_extract_imports, mock_pyinstaller_run):
# Test building an executable with a raw script
build_executable(
script_path=self.script_path,
name="TestApp",
script_type="raw",
raw_script_path=None,
icon=self.icon_path,
pyinstaller_options=["--onefile"],
streamlit_options=["--theme.base=dark"],
Expand All @@ -77,31 +76,6 @@ def test_build_executable_raw(self, mock_extract_imports, mock_pyinstaller_run):
self.assertIn("--add-data", args)
self.assertIn(f"{self.script_path}:.", args)

@patch("streamlit_desktop_app.build.PyInstaller.__main__.run")
def test_build_executable_wrapped(self, mock_pyinstaller_run):
# Test building an executable with a wrapped script
wrapped_script = os.path.join(self.temp_dir, "wrapped_script.py")
with open(wrapped_script, "w") as f:
f.write("print('Wrapped script running')")

build_executable(
script_path=wrapped_script,
name="TestApp",
script_type="wrapped",
raw_script_path=self.script_path,
icon=self.icon_path,
pyinstaller_options=["--noconfirm"],
streamlit_options=None,
)

# Ensure PyInstaller's run method is called with the correct arguments
mock_pyinstaller_run.assert_called_once()
args = mock_pyinstaller_run.call_args[0][0]
self.assertIn("--noconfirm", args)
self.assertIn("--name", args)
self.assertIn("TestApp", args)
self.assertIn("--add-data", args)
self.assertIn(f"{wrapped_script}:.", args)

@patch("streamlit_desktop_app.build.os.path.exists", return_value=False)
def test_missing_script(self, mock_exists):
Expand All @@ -110,44 +84,12 @@ def test_missing_script(self, mock_exists):
build_executable(
script_path="missing_script.py",
name="TestApp",
script_type="raw",
icon=None,
pyinstaller_options=None,
streamlit_options=None,
)
self.assertEqual(str(cm.exception), "Error: The script 'missing_script.py' does not exist.")

def test_invalid_script_type(self):
# Test behavior with an invalid script type
with self.assertRaises(SystemExit) as cm:
build_executable(
script_path=self.script_path,
name="TestApp",
script_type="invalid",
icon=None,
pyinstaller_options=None,
streamlit_options=None,
)
self.assertEqual(str(cm.exception), "Error: Invalid script type 'invalid'. Use 'raw' or 'wrapped'.")

def test_missing_raw_script_path(self):
# Test behavior when raw script path is missing for wrapped scripts
wrapped_script = os.path.join(self.temp_dir, "wrapped_script.py")
with open(wrapped_script, "w") as f:
f.write("print('Wrapped script running')")

with self.assertRaises(SystemExit) as cm:
build_executable(
script_path=wrapped_script,
name="TestApp",
script_type="wrapped",
raw_script_path=None,
icon=None,
pyinstaller_options=None,
streamlit_options=None,
)
self.assertEqual(str(cm.exception), "Error: --raw-script-path must be provided for wrapped scripts.")


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

0 comments on commit 271f05e

Please sign in to comment.