Skip to content

Commit 259b5e6

Browse files
author
Stéphane Philippart
authored
feat: Several python configuration files (#144)
* test: reproduce the problem see #139 * feat: return the fisrt attribute version find
1 parent d27f927 commit 259b5e6

File tree

2 files changed

+95
-10
lines changed

2 files changed

+95
-10
lines changed

src/main/java/io/jenkins/plugins/conventionalcommits/utils/PythonProjectType.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.Scanner;
1212
import org.apache.commons.lang.NotImplementedException;
13+
import org.apache.commons.lang.StringUtils;
1314

1415
/**
1516
* Represent a python project type. Projects any of the having following files are supported: 1.
@@ -47,11 +48,14 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)
4748

4849
String result = "";
4950

51+
// 1- Check if a version attribute is in a setup.py
5052
if (checkSetupPy(directory)) {
5153
List<String> command = Arrays.asList(commandName, "setup.py", "--version");
52-
result = processHelper.runProcessBuilder(directory, command).trim();
53-
} else if (checkSetupCfg(directory)) {
54+
result = processHelper.runProcessBuilder(directory, command);
55+
}
5456

57+
// 2- If no version attribute in a setup.py, check in a setup.cfg
58+
if (StringUtils.isBlank(result) && checkSetupCfg(directory)) {
5559
String filePath = directory.getAbsolutePath() + File.separator + "setup.cfg";
5660
File setupCfg = new File(filePath);
5761
Scanner scanner = new Scanner(setupCfg, StandardCharsets.UTF_8.name());
@@ -60,20 +64,21 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)
6064
String line = scanner.nextLine();
6165
if (line.toLowerCase().contains("version")) {
6266
String[] words = line.split("=");
63-
result = words[1].trim();
67+
result = words[1];
6468
break;
6569
}
6670
}
67-
68-
} else if (checkPyProjectToml(directory)) {
71+
}
72+
73+
// 3- If no version attribute in a setup.py or in a setup.cfg check in a pyproject.toml
74+
if (StringUtils.isBlank(result) && checkPyProjectToml(directory)) {
6975
String tomlFilePath = directory.getAbsolutePath() + File.separator + "pyproject.toml";
7076
result = new PyProjectToml().getVersion(tomlFilePath);
71-
72-
} else {
77+
} else if (StringUtils.isBlank(result)) {
7378
throw new NotImplementedException("Project not supported");
7479
}
7580

76-
return Version.valueOf(result);
81+
return Version.valueOf(result.trim());
7782
}
7883

7984
/**

src/test/java/io/jenkins/plugins/conventionalcommits/utils/PythonProjectTypeTest.java

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.github.zafarkhaja.semver.Version;
44
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.hamcrest.core.IsEqual;
57
import org.junit.Rule;
68
import org.junit.Test;
79
import org.junit.rules.TemporaryFolder;
@@ -16,6 +18,8 @@
1618

1719
import static org.hamcrest.MatcherAssert.assertThat;
1820
import static org.hamcrest.core.StringContains.containsString;
21+
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.Mockito.when;
1923

2024
@RunWith(MockitoJUnitRunner.class)
2125
public class PythonProjectTypeTest {
@@ -57,7 +61,7 @@ private void createSetupPy(File pyDir, String version) throws Exception {
5761
String configContent =
5862
"[metadata]\n"+
5963
"name = myName\n" +
60-
"version = " + version + "\n"+
64+
(StringUtils.isNotBlank(version) ? "version = " + version + "\n" : "")+
6165
"author = EG";
6266
FileWriter pyWriter = new FileWriter(pyCfg);
6367
pyWriter.write(configContent);
@@ -85,7 +89,7 @@ private void createTomlPy(File pyDir, String version) throws Exception {
8589
"[project]\n"+
8690
"name = \"infer_pyproject\"\n" +
8791
"version = \"" + version + "\"\n"+
88-
"author = EG";
92+
"author = [\"EG<[email protected]>\"]";
8993
FileWriter pyWriter = new FileWriter(pyCfg);
9094
pyWriter.write(configContent);
9195
pyWriter.close();
@@ -104,6 +108,82 @@ private void createTomlPyWithIdent(File pyDir, String version) throws Exception
104108
pyWriter.close();
105109
}
106110

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+
107187
@Test
108188
public void shouldWriteVersionBack() throws Exception {
109189
// Set python project

0 commit comments

Comments
 (0)