Skip to content

Commit

Permalink
Implement EnvPathParser, Test, maven.yml Reconfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth committed Dec 6, 2023
1 parent e7740c6 commit fd3a56e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 42 deletions.
41 changes: 15 additions & 26 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ simplify your development tasks.
</dependency>
```

## 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

Expand Down
Binary file added docs/resources/EnvPathParserUseCase.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -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<String> inputCopy) {
try {
return Paths.get(inputCopy.get()).toString();
} catch (InvalidPathException ipe) {
return inputCopy.get();
}
}

private static void parseIfMys(AtomicReference<String> inputCopy,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
}
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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("")
);
}
}

0 comments on commit fd3a56e

Please sign in to comment.