2
2
3
3
import com .github .zafarkhaja .semver .Version ;
4
4
import io .jenkins .plugins .conventionalcommits .process .ProcessHelper ;
5
+ import org .apache .commons .lang3 .StringUtils ;
6
+ import org .hamcrest .core .IsEqual ;
5
7
import org .junit .Rule ;
6
8
import org .junit .Test ;
7
9
import org .junit .rules .TemporaryFolder ;
16
18
17
19
import static org .hamcrest .MatcherAssert .assertThat ;
18
20
import static org .hamcrest .core .StringContains .containsString ;
21
+ import static org .mockito .ArgumentMatchers .any ;
22
+ import static org .mockito .Mockito .when ;
19
23
20
24
@ RunWith (MockitoJUnitRunner .class )
21
25
public class PythonProjectTypeTest {
@@ -57,7 +61,7 @@ private void createSetupPy(File pyDir, String version) throws Exception {
57
61
String configContent =
58
62
"[metadata]\n " +
59
63
"name = myName\n " +
60
- "version = " + version + "\n " +
64
+ ( StringUtils . isNotBlank ( version ) ? "version = " + version + "\n " : "" ) +
61
65
"author = EG" ;
62
66
FileWriter pyWriter = new FileWriter (pyCfg );
63
67
pyWriter .write (configContent );
@@ -85,7 +89,7 @@ private void createTomlPy(File pyDir, String version) throws Exception {
85
89
"[project]\n " +
86
90
"name = \" infer_pyproject\" \n " +
87
91
"version = \" " + version + "\" \n " +
88
- "author = EG " ;
92
+ "author = [ \" EG<[email protected] > \" ] " ;
89
93
FileWriter pyWriter = new FileWriter (pyCfg );
90
94
pyWriter .write (configContent );
91
95
pyWriter .close ();
@@ -104,6 +108,82 @@ private void createTomlPyWithIdent(File pyDir, String version) throws Exception
104
108
pyWriter .close ();
105
109
}
106
110
111
+
112
+ @ Test
113
+ public void shouldGetCurrentVersionForASetupPy () throws Exception {
114
+ // Given a Python project with a setup.py
115
+ File pyDir = rootFolder .newFolder ("SamplePyProject" );
116
+ createSetupPy (pyDir , "0.9.0" );
117
+ when (mockProcessHelper .runProcessBuilder (any (), any ())).thenReturn ("0.9.0" );
118
+
119
+ // Asking to have the current version of the project
120
+ PythonProjectType pyProjectType = new PythonProjectType ();
121
+ Version readVersion = pyProjectType .getCurrentVersion (pyDir , mockProcessHelper );
122
+
123
+ // The current version is returned
124
+ assertThat (readVersion , IsEqual .equalTo (Version .valueOf ("0.9.0" )));
125
+ }
126
+
127
+ @ Test
128
+ public void shouldGetCurrentVersionForASetupCfg () throws Exception {
129
+ // Given a Python project with a setup.cfg
130
+ File pyDir = rootFolder .newFolder ("SamplePyProject" );
131
+ createPythonCfg (pyDir , "0.9.0" );
132
+
133
+ // Asking to have the current version of the project
134
+ PythonProjectType pyProjectType = new PythonProjectType ();
135
+ Version readVersion = pyProjectType .getCurrentVersion (pyDir , mockProcessHelper );
136
+
137
+ // The current version is returned
138
+ assertThat (readVersion , IsEqual .equalTo (Version .valueOf ("0.9.0" )));
139
+ }
140
+
141
+ @ Test
142
+ public void shouldGetCurrentVersionForASetupToml () throws Exception {
143
+ // Given a Python project with a pyproject.toml
144
+ File pyDir = rootFolder .newFolder ("SamplePyProject" );
145
+ createTomlPy (pyDir , "0.9.0" );
146
+
147
+ // Asking to have the current version of the project
148
+ PythonProjectType pyProjectType = new PythonProjectType ();
149
+ Version readVersion = pyProjectType .getCurrentVersion (pyDir , mockProcessHelper );
150
+
151
+ // The current version is returned
152
+ assertThat (readVersion , IsEqual .equalTo (Version .valueOf ("0.9.0" )));
153
+ }
154
+
155
+ @ Test
156
+ public void shouldGetCurrentVersionWithSeveralConfigFilesWithEmptyValues () throws Exception {
157
+ // Given a Python project with a pyproject.toml (with version) and a setup.py (without version)
158
+ File pyDir = rootFolder .newFolder ("SamplePyProject" );
159
+ createTomlPy (pyDir , "0.9.0" );
160
+ createSetupPy (pyDir , null );
161
+
162
+ // Asking to have the current version of the project
163
+ PythonProjectType pyProjectType = new PythonProjectType ();
164
+ Version readVersion = pyProjectType .getCurrentVersion (pyDir , mockProcessHelper );
165
+
166
+ // The current version is returned
167
+ assertThat (readVersion , IsEqual .equalTo (Version .valueOf ("0.9.0" )));
168
+ }
169
+
170
+ @ Test
171
+ public void shouldGetCurrentVersionWithSeveralConfigFilesFirstConfigFileMatche () throws Exception {
172
+ // Given a Python project with a pyproject.toml (with version) and a setup.py (without version)
173
+ File pyDir = rootFolder .newFolder ("SamplePyProject" );
174
+ createTomlPy (pyDir , "0.9.0" );
175
+ createSetupPy (pyDir , "0.8.0" );
176
+ when (mockProcessHelper .runProcessBuilder (any (), any ())).thenReturn ("0.8.0" );
177
+
178
+ // Asking to have the current version of the project
179
+ PythonProjectType pyProjectType = new PythonProjectType ();
180
+ Version readVersion = pyProjectType .getCurrentVersion (pyDir , mockProcessHelper );
181
+
182
+ // The current version is returned
183
+ assertThat (readVersion , IsEqual .equalTo (Version .valueOf ("0.8.0" )));
184
+ }
185
+
186
+
107
187
@ Test
108
188
public void shouldWriteVersionBack () throws Exception {
109
189
// Set python project
0 commit comments