diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 6157708..3c2f912 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -17,11 +17,13 @@ on: jobs: test: - + if: github.ref != 'refs/heads/main' runs-on: ubuntu-latest - + env: + HOME: /path/to/test steps: - - uses: actions/checkout@v3 + - name: Checkout code + uses: actions/checkout@v4 - name: Set up JDK 1.8 uses: actions/setup-java@v3 with: @@ -32,35 +34,22 @@ jobs: run: mvn -B --update-snapshots clean test build: - + if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up JDK 1.8 uses: actions/setup-java@v3 with: java-version: '8' distribution: 'corretto' cache: maven - - name: Build with Maven - run: mvn -B package --file pom.xml - -# create_tag: -# -# runs-on: ubuntu-latest -# -# needs: build -# -# if: github.ref == 'refs/heads/main' -# -# steps: -# - name: Get version from pom.xml -# run: echo "##[set-output name=version]$(xmllint --xpath 'string(/project/version)' pom.xml)" -# id: get_version -# - name: Create tag -# run: | -# echo "Creating tag v${{ steps.get_version.outputs.version }}" -# git tag -a v${{ steps.get_version.outputs.version }} -m "Release v${{ steps.get_version.outputs.version }}" -# git push origin v${{ steps.get_version.outputs.version }} -# + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + - name: Publish package + run: mvn --batch-mode clean deploy + env: + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 639e2fd..65cc41a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `EnvPathParser` first implementation and JavaDoc -- `EnvPathParser` unit test initialization +- `EnvPathParser` simple unit tests +- `ParametersProvider` in test package to facilitate testing with several, different parameters. + +### Changed + +- Reworked GitHub `maven.yml` to only test in non-production branches and compile, test, package and deploy on + production branches. +- README.md Update ### Changed diff --git a/README.md b/README.md index 430d599..771881a 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,13 @@ simplify your development tasks. ``` -## Featuring +## Tools **XmlHandler** retrieves values from a doc when provided with the target XPath. -Why parse through a whole XML just to retrieve one value? We can use the power of tooler-tools and XPath. There's no -management or error handling. +**EnvPathParser** parses and expands env path variables into file paths. + +![EnvPathParserUseCase.png](docs/resources/EnvPathParserUseCase.png) ### Example - Get String From XPath diff --git a/docs/resources/EnvPathParserUseCase.png b/docs/resources/EnvPathParserUseCase.png new file mode 100644 index 0000000..3457386 Binary files /dev/null and b/docs/resources/EnvPathParserUseCase.png differ diff --git a/src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java b/src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java index 419320d..44c5a8b 100644 --- a/src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java +++ b/src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java @@ -1,5 +1,7 @@ package pt.codeforge.toolertools.pathfinder; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -26,7 +28,9 @@ private EnvPathParser() { * Parses the input path by replacing environment variable placeholders with their respective values. Environment * variable placeholders are denoted by percent signs ('%') on both ends for Windows and by a dollar sign ('$') at * the beginning for Unix-like systems. The method handles both styles. After parsing, the method returns the - * normalized path as a String. + * normalized path as a String, or if when path string cannot be converted into a {@link Path} because the path + * string contains invalid characters, or the path string is invalid for other file system specific reasons, it + * returns the path as literally converted. * * @param input The input path to be parsed. Must not be null. * @return The parsed and normalized path as a String. @@ -42,7 +46,15 @@ public static String getEnvPath(String input) { parseIfMys(inputCopy, pathComponentsList); parseIfUnix(inputCopy, pathComponentsList); - return Paths.get(inputCopy.get()).toString(); + return getPathOrDefault(inputCopy); + } + + private static String getPathOrDefault(AtomicReference inputCopy) { + try { + return Paths.get(inputCopy.get()).toString(); + } catch (InvalidPathException ipe) { + return inputCopy.get(); + } } private static void parseIfMys(AtomicReference inputCopy, diff --git a/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java b/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java index 293f663..532721c 100644 --- a/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java +++ b/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java @@ -1,28 +1,47 @@ package pt.codeforge.toolertools.pathfinder; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class EnvPathParserTest { - @BeforeEach - void setup() { - System.setProperty("$HOME", "test"); + @ParameterizedTest + @ValueSource(strings = "$HOME") + void givenUnixValidEnvVariable_testgetEnvPath_shouldReturnValidPath(String input) { + String result = EnvPathParser.getEnvPath(input); + + assertValidResult(input, result); + } + + @ParameterizedTest + @ValueSource(strings = "$HOME/$TEMP") + void givenUnixMultipleValidEnvVariables_testgetEnvPath_shouldReturnValidPath(String input) { + String result = EnvPathParser.getEnvPath(input); + + assertValidResult(input, result); } @ParameterizedTest - @ValueSource(strings = "$HOMEDRIVE/$HOMEPATH") - void testGetEnvPath(String input) { + @ValueSource(strings = "$HOME/%HOME%") + void givenUnixAndMysMultipleValidEnvVariables_testgetEnvPath_shouldReturnValidPath(String input) { + String result = EnvPathParser.getEnvPath(input); - System.out.println(EnvPathParser.getEnvPath(input)); + assertValidResult(input, result); } @Test - void testBadGetEnvPath() { - Assertions.assertThrows(IllegalArgumentException.class, () -> EnvPathParser.getEnvPath(null)); + void givenNullInput_testGetEnvPath_shouldThrowIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> EnvPathParser.getEnvPath(null), + "EnvPathParser#getEnvPath should throw IllegalArgumentException.class."); } + private static void assertValidResult(String input, String result) { + assertNotEquals(input, result, "Input should not be equal to result."); + assertFalse(result.isEmpty(), "Result should not be empty."); + } } diff --git a/src/test/java/pt/codeforge/toolertools/utils/ParametersProvider.java b/src/test/java/pt/codeforge/toolertools/utils/ParametersProvider.java new file mode 100644 index 0000000..7420b10 --- /dev/null +++ b/src/test/java/pt/codeforge/toolertools/utils/ParametersProvider.java @@ -0,0 +1,21 @@ +package pt.codeforge.toolertools.utils; + +import java.util.stream.Stream; +import org.junit.jupiter.params.provider.Arguments; + +public class ParametersProvider { + + public static final String PARAMETERS_PROVIDER_CLASS = "pt.codeforge.toolertools.utils.ParametersProvider"; + + public static Stream getEnvPaths(){ + return Stream.of( + Arguments.of("$HOMEDRIVE/$HOMEPATH"), + Arguments.of("$TEMP/test"), + Arguments.of("test/$HOMEPATH/"), + Arguments.of("../$HOMEPATH/"), + Arguments.of("./$HOMEPATH/"), + Arguments.of("$HOMEDRIVE/$HOMEPATH/test"), + Arguments.of("") + ); + } +}