Skip to content

Commit 0c1b8d6

Browse files
Add comprehensive language_version_manager tests
Tests verify correct Python version selection behavior: - Lowest compatible version for range constraints - Correct handling of exact versions - Default to highest when no version specified - Support for both PEP 621 and Poetry formats Co-authored-by: AbhishekBhaskar <22154418+AbhishekBhaskar@users.noreply.github.com>
1 parent 6652fc5 commit 0c1b8d6

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# typed: false
2+
# frozen_string_literal: true
3+
4+
require "spec_helper"
5+
require "dependabot/python/language_version_manager"
6+
require "dependabot/python/file_parser/python_requirement_parser"
7+
require "dependabot/dependency_file"
8+
9+
RSpec.describe Dependabot::Python::LanguageVersionManager do
10+
let(:manager) { described_class.new(python_requirement_parser: parser) }
11+
let(:parser) { Dependabot::Python::FileParser::PythonRequirementParser.new(dependency_files: files) }
12+
13+
describe "#python_version" do
14+
subject(:python_version) { manager.python_version }
15+
16+
context "with pyproject.toml containing requires-python" do
17+
let(:files) { [pyproject_file] }
18+
let(:pyproject_file) do
19+
Dependabot::DependencyFile.new(
20+
name: "pyproject.toml",
21+
content: pyproject_content
22+
)
23+
end
24+
25+
context "when requires-python specifies a range" do
26+
let(:pyproject_content) do
27+
<<~TOML
28+
[project]
29+
name = "test-package"
30+
requires-python = ">= 3.9, <3.13"
31+
TOML
32+
end
33+
34+
it "selects the lowest compatible Python version" do
35+
expect(python_version).to eq("3.9.24")
36+
end
37+
end
38+
39+
context "when requires-python specifies minimum only" do
40+
let(:pyproject_content) do
41+
<<~TOML
42+
[project]
43+
name = "test-package"
44+
requires-python = ">= 3.10"
45+
TOML
46+
end
47+
48+
it "selects the lowest compatible Python version" do
49+
expect(python_version).to eq("3.10.19")
50+
end
51+
end
52+
53+
context "when requires-python specifies exact version" do
54+
let(:pyproject_content) do
55+
<<~TOML
56+
[project]
57+
name = "test-package"
58+
requires-python = "3.11"
59+
TOML
60+
end
61+
62+
it "selects Python 3.11" do
63+
expect(python_version).to eq("3.11.14")
64+
end
65+
end
66+
67+
context "with Poetry format python dependency" do
68+
let(:pyproject_content) do
69+
<<~TOML
70+
[tool.poetry.dependencies]
71+
python = "^3.12"
72+
TOML
73+
end
74+
75+
it "selects the lowest compatible Python version" do
76+
expect(python_version).to eq("3.12.12")
77+
end
78+
end
79+
80+
context "without Python version specified" do
81+
let(:pyproject_content) do
82+
<<~TOML
83+
[project]
84+
name = "test-package"
85+
TOML
86+
end
87+
88+
it "defaults to the highest available Python version" do
89+
expect(python_version).to eq("3.14.0")
90+
end
91+
end
92+
end
93+
94+
context "with no dependency files" do
95+
let(:files) { [] }
96+
97+
it "defaults to the highest available Python version" do
98+
expect(python_version).to eq("3.14.0")
99+
end
100+
end
101+
end
102+
103+
describe "#python_requirement_string" do
104+
subject(:requirement_string) { manager.python_requirement_string }
105+
106+
context "with pyproject.toml containing requires-python" do
107+
let(:files) { [pyproject_file] }
108+
let(:pyproject_file) do
109+
Dependabot::DependencyFile.new(
110+
name: "pyproject.toml",
111+
content: <<~TOML
112+
[project]
113+
name = "test-package"
114+
requires-python = ">= 3.9, <3.13"
115+
TOML
116+
)
117+
end
118+
119+
it "returns the requirement string" do
120+
expect(requirement_string).to eq(">= 3.9, <3.13")
121+
end
122+
end
123+
124+
context "without Python version specified" do
125+
let(:files) { [] }
126+
127+
it "returns the highest version" do
128+
expect(requirement_string).to eq("3.14.0")
129+
end
130+
end
131+
end
132+
133+
describe "#python_major_minor" do
134+
subject(:major_minor) { manager.python_major_minor }
135+
136+
context "with requires-python = '>= 3.9, <3.13'" do
137+
let(:files) do
138+
[
139+
Dependabot::DependencyFile.new(
140+
name: "pyproject.toml",
141+
content: <<~TOML
142+
[project]
143+
requires-python = ">= 3.9, <3.13"
144+
TOML
145+
)
146+
]
147+
end
148+
149+
it "returns the major.minor version" do
150+
expect(major_minor).to eq("3.9")
151+
end
152+
end
153+
end
154+
end

0 commit comments

Comments
 (0)