Skip to content

Commit

Permalink
feat: Several python configuration files (#144)
Browse files Browse the repository at this point in the history
* test: reproduce the problem see #139

* feat: return the fisrt attribute version find
  • Loading branch information
Stéphane Philippart authored Nov 9, 2021
1 parent d27f927 commit 259b5e6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Scanner;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;

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

String result = "";

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

// 2- If no version attribute in a setup.py, check in a setup.cfg
if (StringUtils.isBlank(result) && checkSetupCfg(directory)) {
String filePath = directory.getAbsolutePath() + File.separator + "setup.cfg";
File setupCfg = new File(filePath);
Scanner scanner = new Scanner(setupCfg, StandardCharsets.UTF_8.name());
Expand All @@ -60,20 +64,21 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)
String line = scanner.nextLine();
if (line.toLowerCase().contains("version")) {
String[] words = line.split("=");
result = words[1].trim();
result = words[1];
break;
}
}

} else if (checkPyProjectToml(directory)) {
}

// 3- If no version attribute in a setup.py or in a setup.cfg check in a pyproject.toml
if (StringUtils.isBlank(result) && checkPyProjectToml(directory)) {
String tomlFilePath = directory.getAbsolutePath() + File.separator + "pyproject.toml";
result = new PyProjectToml().getVersion(tomlFilePath);

} else {
} else if (StringUtils.isBlank(result)) {
throw new NotImplementedException("Project not supported");
}

return Version.valueOf(result);
return Version.valueOf(result.trim());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.core.IsEqual;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -16,6 +18,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PythonProjectTypeTest {
Expand Down Expand Up @@ -57,7 +61,7 @@ private void createSetupPy(File pyDir, String version) throws Exception {
String configContent =
"[metadata]\n"+
"name = myName\n" +
"version = " + version + "\n"+
(StringUtils.isNotBlank(version) ? "version = " + version + "\n" : "")+
"author = EG";
FileWriter pyWriter = new FileWriter(pyCfg);
pyWriter.write(configContent);
Expand Down Expand Up @@ -85,7 +89,7 @@ private void createTomlPy(File pyDir, String version) throws Exception {
"[project]\n"+
"name = \"infer_pyproject\"\n" +
"version = \"" + version + "\"\n"+
"author = EG";
"author = [\"EG<foo@foo.com>\"]";
FileWriter pyWriter = new FileWriter(pyCfg);
pyWriter.write(configContent);
pyWriter.close();
Expand All @@ -104,6 +108,82 @@ private void createTomlPyWithIdent(File pyDir, String version) throws Exception
pyWriter.close();
}


@Test
public void shouldGetCurrentVersionForASetupPy() throws Exception {
// Given a Python project with a setup.py
File pyDir = rootFolder.newFolder("SamplePyProject");
createSetupPy(pyDir, "0.9.0");
when(mockProcessHelper.runProcessBuilder(any(), any())).thenReturn("0.9.0");

// Asking to have the current version of the project
PythonProjectType pyProjectType = new PythonProjectType();
Version readVersion = pyProjectType.getCurrentVersion(pyDir, mockProcessHelper);

// The current version is returned
assertThat(readVersion, IsEqual.equalTo(Version.valueOf("0.9.0")));
}

@Test
public void shouldGetCurrentVersionForASetupCfg() throws Exception {
// Given a Python project with a setup.cfg
File pyDir = rootFolder.newFolder("SamplePyProject");
createPythonCfg(pyDir, "0.9.0");

// Asking to have the current version of the project
PythonProjectType pyProjectType = new PythonProjectType();
Version readVersion = pyProjectType.getCurrentVersion(pyDir, mockProcessHelper);

// The current version is returned
assertThat(readVersion, IsEqual.equalTo(Version.valueOf("0.9.0")));
}

@Test
public void shouldGetCurrentVersionForASetupToml() throws Exception {
// Given a Python project with a pyproject.toml
File pyDir = rootFolder.newFolder("SamplePyProject");
createTomlPy(pyDir, "0.9.0");

// Asking to have the current version of the project
PythonProjectType pyProjectType = new PythonProjectType();
Version readVersion = pyProjectType.getCurrentVersion(pyDir, mockProcessHelper);

// The current version is returned
assertThat(readVersion, IsEqual.equalTo(Version.valueOf("0.9.0")));
}

@Test
public void shouldGetCurrentVersionWithSeveralConfigFilesWithEmptyValues() throws Exception {
// Given a Python project with a pyproject.toml (with version) and a setup.py (without version)
File pyDir = rootFolder.newFolder("SamplePyProject");
createTomlPy(pyDir, "0.9.0");
createSetupPy(pyDir, null);

// Asking to have the current version of the project
PythonProjectType pyProjectType = new PythonProjectType();
Version readVersion = pyProjectType.getCurrentVersion(pyDir, mockProcessHelper);

// The current version is returned
assertThat(readVersion, IsEqual.equalTo(Version.valueOf("0.9.0")));
}

@Test
public void shouldGetCurrentVersionWithSeveralConfigFilesFirstConfigFileMatche() throws Exception {
// Given a Python project with a pyproject.toml (with version) and a setup.py (without version)
File pyDir = rootFolder.newFolder("SamplePyProject");
createTomlPy(pyDir, "0.9.0");
createSetupPy(pyDir, "0.8.0");
when(mockProcessHelper.runProcessBuilder(any(), any())).thenReturn("0.8.0");

// Asking to have the current version of the project
PythonProjectType pyProjectType = new PythonProjectType();
Version readVersion = pyProjectType.getCurrentVersion(pyDir, mockProcessHelper);

// The current version is returned
assertThat(readVersion, IsEqual.equalTo(Version.valueOf("0.8.0")));
}


@Test
public void shouldWriteVersionBack() throws Exception {
// Set python project
Expand Down

0 comments on commit 259b5e6

Please sign in to comment.