Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for passing a settings from my test resources #233

Open
filiphr opened this issue Jan 17, 2022 · 11 comments
Open

Support for passing a settings from my test resources #233

filiphr opened this issue Jan 17, 2022 · 11 comments
Labels
enhancement New feature or request

Comments

@filiphr
Copy link

filiphr commented Jan 17, 2022

I would like to be able to use a custom settings file available in my resources. I found MavenCLIOptions.SETTINGS. However, from what I could deduce, this does not allow me to point to a file in my test resources.

I would like to be able to reference a file from my test resources and use that.

We have made this work for us by adding a new @MavenSettings annotation. However, I think that it would be better if we can expand @MavenOption with a new method resource that would be able to resolve the full path to a test resource and use it in the tests. We are ready to work on this contribution to the project in case you are interested in this functionality.

@filiphr filiphr added the enhancement New feature or request label Jan 17, 2022
@khmarbaise
Copy link
Owner

Could you please make a full working example and described more in detail what exactly the purpose of such setup is?

@filiphr
Copy link
Author

filiphr commented Jan 18, 2022

For our use case we would like to be able to test access to our artifacts from our private repository with different user flavours. This means that all we need to have a settings.xml that we can configure with access to our repository and be able to change the user through a system property.

e.g. settings.xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>my-mirror</id>
            <username>${MAVEN_USER}</username>
            <password>${MAVEN_PASSWORD}</password>
        </server>
    </servers>
    <profiles>
        <profile>
            <id>my-artifacts</id>
            <repositories>
                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>my-mirror</id>
                    <name>my-maven-all</name>
                    <url>${MY_MAVEN_REPO_URL}</url>
                </repository>
                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>central</name>
                    <url>https://repo1.maven.org/maven2</url>
                </repository>

            </repositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>my-artifacts</activeProfile>
    </activeProfiles>
</settings>

a test case then looks like:

@ExtendWith(MavenITExtension.class)
@MavenSourceProject("custom")
@MavenSettings("settings-test.xml")
class CustomAccessIT {

    @MavenTest
    @MavenGoal({ "clean", "verify" })
    @SystemProperty(value = "MAVEN_USER", content = "base-reader")
    void baseReader(MavenExecutionResult result) {
        assertThat(result).isFailure();
        assertThat(result)
                .out()
                .error()
                .anySatisfy(error -> {
                    Assertions.assertThat(error)
                            .contains(
                                    "403 Forbidden"
                            );
                });
    }

    @MavenTest
    @MavenGoal({ "clean", "verify" })
    @SystemProperty(value = "MAVEN_USER", content = "custom-reader")
    void customReader(MavenExecutionResult result) {
        assertThat(result).isFailure();
    }

}

Does this help explain the use case I am trying to achieve?

@faltfe
Copy link

faltfe commented Aug 27, 2022

Providing a settings.xml is needed, when dependencies are fetched from a custom Maven repository or running the tests behind a proxy server.

@khmarbaise
Copy link
Owner

@faltfe You should already have a settings.xml in your own $HOME/.m2/ directory if you consume from an internal repository manager or behind a proxy.

@filiphr
Copy link
Author

filiphr commented Sep 26, 2022

@khmarbaise what you are saying is right and things should work like that. However, the use case that I am trying to achieve is not the one that @faltfe explained.

As I've mentioned earlier, I am more than happy to work on a contribution that is going to provide this functionality if you are ready to accept something like this.

@khmarbaise
Copy link
Owner

@filiphr Yes I know. I'm already thinking about a design to solve that...

@filiphr
Copy link
Author

filiphr commented Sep 26, 2022

That's great to hear @khmarbaise. We already have something on our side using the examples I provided in #233 (comment) (the use of @MavenSettings). I can provide a PR if you think that the approach is right.

The alternative would be to support using resources in @MavenOption. Can provide a PR using this approach as well

@sparsick
Copy link
Sponsor Contributor

sparsick commented Nov 4, 2022

I tried to use a custom settings.xml via @MavanOption (see below sample code)

    @MavenTest
    @MavenGoal("\${project.groupId}:\${project.artifactId}:\${project.version}:update")
    @MavenOption("--settings settings.xml")
    fun jgitProviderIsSet_scmConfigIsSet(result: MavenExecutionResult) {
    }

but it produces the following error:

// output of mvn-stderr.log
Unable to parse command line options: Unrecognized option: --settings settings.xml

// output of mvn-argument.log
/home/sparsick/.sdkman/candidates/maven/current/bin/mv
-Dmaven.repo.local=/home/sparsick/dev/workspace/dependency-update-maven-plugin/target/maven-it/io/github/georgberky/maven/plugins/depsupdate/UpdateMojoJGit2IT/jgitProviderIsSet_scmConfigIsSet/.m2/repository
--settings settings.xml
io.github.georgberky.maven.plugins.depsupdate:dependency-update-maven-plugin:0.9.0-SNAPSHOT:update

Used Maven version is 3.8.6

The whole sample can be found here.

Maybe this sample helps you.

@khmarbaise
Copy link
Owner

khmarbaise commented Nov 19, 2022

@maventest
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:update")
@MavenOption("--settings settings.xml")
fun jgitProviderIsSet_scmConfigIsSet(result: MavenExecutionResult) {
}

If you like to use that you should use it like this:

  @MavenOption(value = MavenCLIOptions.SETTINGS, parameter = "settings.xml")

Or you can use:

  @MavenOption(value = "--settings", parameter = "settings.xml")

@sparsick
Copy link
Sponsor Contributor

Thanks, that works for me.

It wasn't clear for me reading the docs, that MavenOption can be used also in your shown sample.

@khmarbaise
Copy link
Owner

Thanks, that works for me.

It wasn't clear for me reading the docs, that MavenOption can be used also in your shown sample.

That means the docs needs to be improved...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants