Skip to content

Commit

Permalink
Merge pull request #44 from caarlos0/new
Browse files Browse the repository at this point in the history
clitest and bugfixes
  • Loading branch information
caarlos0 committed Feb 25, 2016
2 parents f0964f8 + 1f86b56 commit 04371c8
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 59 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ And, yes, this strategy (based on `jvm config`) can make `jvm` work on Windows
with any `bash` terminal too. Or any other operating system with a POSIX shell
really.

You can also read the [self-document tests][tests] to find out more usages.

[tests]: /tests/test.clitest.md

### Antigen/Antibody

For those using Antigen, Antibody or whatever, just bundle `caarlos0/jvm`, as
Expand Down
27 changes: 18 additions & 9 deletions jvm.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/bin/sh
# find the appropriate JAVA_HOME for the given java version and fix PATH.
__jvm_set() {
# finds the java home for the given version
__jvm_javahome() {
version="$1"
previous="$JAVA_HOME"

# custom jdk strategy
test -f ~/.jvmconfig &&
new="$(grep "$version"= ~/.jvmconfig || true | cut -f2 -d'=')"
new="$(grep "$version"= ~/.jvmconfig | cut -f2 -d'=')"

# ubuntu/debian jdk strategy
test -z "$new" -a -d "/usr/lib/jvm/java-${version}-oracle/" &&
Expand All @@ -18,6 +17,15 @@ __jvm_set() {

# sanity check: new must be a folder.
test -n "$new" -a -d "$new" || return 1
echo "$new"
}

# find the appropriate JAVA_HOME for the given java version and fix PATH.
__jvm_set() {
version="$1"
previous="$JAVA_HOME"

new="$(__jvm_javahome "$version")"

# PATH cleanup
# shellcheck disable=SC2155
Expand All @@ -40,7 +48,10 @@ __jvm_pomversion_evaluate() {

# tried to find the java version using regex.
__jvm_pomversion_regex() {
grep -Eo '<(java.version|maven.compiler.source|source)>1\.[4-9]</.*>' pom.xml |
regex="<(java.version|maven.compiler.source|source)>1\.[4-9]</.*>"
version="$(grep -Eo "$regex" pom.xml)"
test -z "$version" && return 1
echo "$version" |
cut -f2 -d'>' |
cut -f2 -d'.' |
cut -f1 -d'<'
Expand All @@ -49,9 +60,7 @@ __jvm_pomversion_regex() {
# tries multiple strategies to find the java version, and then sets it in a
# .java-version
__jvm_pomversion() {
test -f pom.xml || return 1
version="$(__jvm_pomversion_regex)"
test -z "$version" && version="$(__jvm_pomversion_evaluate)"
version="$(__jvm_pomversion_regex || __jvm_pomversion_evaluate)"
test -n "$version" && echo "$version" > .java-version
}

Expand All @@ -69,7 +78,7 @@ __jvm_user_version() {

# finds out which java version should be used.
__jvm_version() {
test ! -f .java-version && __jvm_pomversion
test ! -f .java-version -a -f pom.xml && __jvm_pomversion
__jvm_local_version || __jvm_user_version
}

Expand Down
14 changes: 14 additions & 0 deletions tests/empty/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jvm</groupId>
<artifactId>jdk8</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../java8</relativePath>
</parent>
<groupId>com.jvm</groupId>
<artifactId>empty</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
3 changes: 2 additions & 1 deletion tests/grep/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jvm</groupId>
<artifactId>jdk8</artifactId>
<artifactId>grep</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
Expand Down
3 changes: 2 additions & 1 deletion tests/java7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<groupId>com.jvm</groupId>
<artifactId>jdk7</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<java.version>1.7</java.version>
</properties>
</project>
1 change: 1 addition & 0 deletions tests/java8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<groupId>com.jvm</groupId>
<artifactId>jdk8</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Expand Down
115 changes: 115 additions & 0 deletions tests/test.clitest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# jvm test suite

This is the self-document test suite for JVM.

# Initial setup

Just cleanup, variable setting and source what needs to be sourced.

```console
$ ROOT="$(pwd)"
$ TESTS="tests"
$ find . -name '.java-version' -delete
$ source jvm.sh
$ export MAVEN_OPTS=""
$
```

# jvm global

Test setting a global version.

```console
$ jvm global 7
$ jvm version
7
$
```

# jvm local

Test setting a version to the current folder.

```console
$ jvm local 7
$ jvm version
7
$
```

# maven.compiler.source

Test a pom with a `maven.compiler.source` property set to Java 8.

```console
$ cd "$ROOT/$TESTS/java8"
$ jvm reload
$ jvm version
8
$
```

# java.version

Test a pom with a `java.version` property set to Java 7.

```console
$ cd "$ROOT/$TESTS/java7"
$ jvm reload
$ jvm version
7
$
```

# maven-compiler-plugin

Test reading the `source` tag from `maven-compiler-plugin`, which in this
example is set to Java 7.

```console
$ cd "$ROOT/$TESTS/grep"
$ jvm reload
$ jvm version
7
$
```

# parent pom

Test a pom with no java config at all, the config is at its parent pom,
therefore, this will run `mvn help:evaluate` and find out that the parent
is using Java 8.

```console
$ cd "$ROOT/$TESTS/empty"
$ jvm reload
$ jvm version
8
$
```

# jvmconfig

Test that `jvm` respects a custom java version in `~/.jvmconfig`, in this case,
pointing out Java 6 to use Java 7 home.

```console
$ cd "$ROOT/$TESTS/grep"
$ rm .java-version
$ echo "6=$(__jvm_javahome 7)" > ~/.jvmconfig
$ jvm local 6
$ jvm reload
$ jvm version
6
$
```

# Cleanup

Remove unneeded files after all tests ran.

```console
$ cd "$ROOT"
$ find . -name '.java-version' -delete
$
```
50 changes: 2 additions & 48 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,3 @@
#!/bin/bash
set -eo pipefail
ROOT="$(pwd)"
TESTS="$(dirname "${BASH_SOURCE[0]}")"

assert_java() {
version="$1"
test "$(jvm version)" = "$version" || exit 1
}

test_pom7() {
cd "$ROOT/$TESTS"/java7
jvm reload
assert_java 7
}

test_pom7_grep() {
cd "$ROOT/$TESTS"/grep
jvm reload
assert_java 7
}

test_pom8() {
cd "$ROOT/$TESTS"/java8
jvm reload
assert_java 8
}

find . -name '.java-version' -delete

# shellcheck disable=SC1091
source jvm.sh

echo "jvm global 8"
jvm global 8
assert_java 8

echo "jvm local 7"
jvm local 7
assert_java 7

echo "cdwd java 8 pom"
test_pom8

echo "cdwd java 7 pom"
test_pom7

echo "cdwd java 7 grep"
test_pom7_grep
curl -s https://raw.githubusercontent.com/aureliojargas/clitest/master/clitest |
bash -s -- ./tests/test.clitest.md

0 comments on commit 04371c8

Please sign in to comment.