@@ -18,7 +18,7 @@ class TestDetectFrameworkAndApp:
1818
1919 def test_no_files_exist (self ):
2020 """Test detection when no app files exist."""
21- with patch ("os.path.exists " , return_value = False ):
21+ with patch ("glob.glob " , return_value = [] ):
2222 result = detect_framework_and_app ()
2323 assert result is None
2424
@@ -28,6 +28,8 @@ def test_no_files_exist(self):
2828 ("FastAPI" , "from fastapi import FastAPI" , "app" , "app.py" , "app" ),
2929 ("Flask" , "from flask import Flask" , "application" , "app.py" , "application" ),
3030 ("FastAPI" , "import fastapi\n from fastapi import FastAPI" , "main" , "main.py" , "main" ),
31+ ("FastAPI" , "from fastapi import FastAPI" , "app" , "src/main.py" , "app" ),
32+ ("Flask" , "from flask import Flask" , "app" , "example/src/main.py" , "app" ),
3133 ],
3234 )
3335 def test_framework_app_detection (self , framework , import_stmt , var_name , expected_file , expected_var ):
@@ -53,10 +55,7 @@ def root():
5355 return "hello"
5456"""
5557
56- def mock_exists (path ):
57- return path == expected_file
58-
59- with patch ("os.path.exists" , side_effect = mock_exists ), patch ("builtins.open" , mock_open (read_data = app_content )):
58+ with patch ("glob.glob" , return_value = [expected_file ]), patch ("builtins.open" , mock_open (read_data = app_content )):
6059 result = detect_framework_and_app ()
6160 assert result == (framework , expected_file , expected_var )
6261
@@ -68,7 +67,7 @@ def test_no_framework_detected(self):
6867def hello():
6968 return "hello"
7069"""
71- with patch ("os.path.exists " , return_value = True ), patch ("builtins.open" , mock_open (read_data = app_content )):
70+ with patch ("glob.glob " , return_value = [ "app.py" ] ), patch ("builtins.open" , mock_open (read_data = app_content )):
7271 result = detect_framework_and_app ()
7372 assert result is None
7473
@@ -79,14 +78,14 @@ def test_framework_but_no_app_variable(self):
7978
8079# No app variable defined
8180"""
82- with patch ("os.path.exists " , return_value = True ), patch ("builtins.open" , mock_open (read_data = app_content )):
81+ with patch ("glob.glob " , return_value = [ "app.py" ] ), patch ("builtins.open" , mock_open (read_data = app_content )):
8382 result = detect_framework_and_app ()
8483 assert result is None
8584
8685 def test_file_read_exception (self ):
8786 """Test handling of file read exceptions."""
8887 with (
89- patch ("os.path.exists " , return_value = True ),
88+ patch ("glob.glob " , return_value = [ "app.py" ] ),
9089 patch ("builtins.open" , side_effect = IOError ("Cannot read file" )),
9190 ):
9291 result = detect_framework_and_app ()
@@ -99,18 +98,19 @@ def test_multiple_files_checked(self):
9998server = Flask(__name__)
10099"""
101100
102- def mock_exists (path ):
103- return path in ["app.py" , "server.py" ]
101+ with patch ("glob.glob" , return_value = ["app.py" , "server.py" ]):
104102
105- def mock_open_handler (path , mode = "r" ):
106- if path == "app.py" :
107- return mock_open (read_data = "# just a comment" )()
108- elif path == "server.py" :
109- return mock_open (read_data = flask_content )()
103+ def mock_open_handler (path , mode = "r" ):
104+ if path == "app.py" :
105+ return mock_open (read_data = "# foobar" )()
106+ elif path == "server.py" :
107+ return mock_open (read_data = flask_content )()
108+ else :
109+ return mock_open (read_data = "" )()
110110
111- with patch ( "os.path.exists" , side_effect = mock_exists ), patch ("builtins.open" , side_effect = mock_open_handler ):
112- result = detect_framework_and_app ()
113- assert result == ("Flask" , "server.py" , "server" )
111+ with patch ("builtins.open" , side_effect = mock_open_handler ):
112+ result = detect_framework_and_app ()
113+ assert result == ("Flask" , "server.py" , "server" )
114114
115115
116116class TestGenerateConftestContent :
@@ -136,6 +136,26 @@ def test_flask_conftest(self):
136136 assert "Provide the Flask app" in content
137137 assert "return application" in content
138138
139+ def test_subdirectory_conftest (self ):
140+ """Test generating conftest for app in subdirectory."""
141+ content = generate_conftest_content ("FastAPI" , "src/main.py" , "app" )
142+
143+ assert "import pytest" in content
144+ assert "from src.main import app" in content
145+ assert "def app():" in content
146+ assert "Provide the FastAPI app" in content
147+ assert "return app" in content
148+
149+ def test_nested_subdirectory_conftest (self ):
150+ """Test generating conftest for app in nested subdirectory."""
151+ content = generate_conftest_content ("Flask" , "example/src/main.py" , "app" )
152+
153+ assert "import pytest" in content
154+ assert "from example.src.main import app" in content
155+ assert "def app():" in content
156+ assert "Provide the Flask app" in content
157+ assert "return app" in content
158+
139159
140160class TestGeneratePyprojectConfig :
141161 """Tests for generate_pyproject_config function."""
0 commit comments