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

#1602 Discover with versions #2228

Merged
merged 22 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void exec() throws IOException {
if (this.central == null) {
this.central = new Central(this.project, this.session, this.manager);
}
String before = this.tojos.status();
String before = this.scopedTojos().status();
int cycle = 0;
final Moja<?>[] mojas = {
new Moja<>(ParseMojo.class),
Expand All @@ -211,7 +211,7 @@ public void exec() throws IOException {
for (final Moja<?> moja : mojas) {
moja.copy(this).execute();
}
final String after = this.tojos.status();
final String after = this.scopedTojos().status();
++cycle;
if (Logger.isInfoEnabled(this)) {
Logger.info(
Expand Down
63 changes: 45 additions & 18 deletions eo-maven-plugin/src/main/java/org/eolang/maven/DiscoverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -94,26 +95,33 @@ public void exec() throws IOException {
private Collection<String> discover(final Path file)
throws FileNotFoundException {
final XML xml = new XMLDocument(file);
final List<String> base = new ListOf<>(
"//o[",
"not(starts-with(@base,'.'))",
"and @base != 'Q'",
"and @base != '^'",
"and @base != '$'",
"and @base != '&'",
"and not(@ref)"
);
final List<String> regular = new ListOf<>(base);
if (this.withVersions) {
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
regular.add("and not(@ver)");
}
regular.add("]/@base");
final Collection<String> names = new TreeSet<>(
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
new ListOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
"//o[",
"not(starts-with(@base,'.'))",
" and @base != 'Q'",
" and @base != '^'",
" and @base != '$'",
" and @base != '&'",
" and not(@ref)",
"]/@base"
)
)
)
)
DiscoverMojo.names(xml, regular)
);
if (this.withVersions) {
final List<String> versioned = new ListOf<>(base);
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
versioned.addAll(
new ListOf<>(
"and @ver",
"]/concat(@base,'|',@ver)"
)
);
names.addAll(DiscoverMojo.names(xml, versioned));
}
if (!xml.nodes("//o[@vararg]").isEmpty()) {
names.add("org.eolang.tuple");
}
Expand All @@ -131,4 +139,23 @@ private Collection<String> discover(final Path file)
return names;
}

/**
* Get list of object names from given XML by provided xpath.
* @param xml XML.
* @param xpath Xpath.
* @return List of object names.
*/
private static List<String> names(final XML xml, final List<String> xpath) {
return new ListOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
xpath
)
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ public void exec() throws IOException {
.withVersion(ParseMojo.ZERO)
.withVer(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered", name);
if (this.external != null) {
this.externalTojos
.add(name)
.withSource(file.toAbsolutePath())
.withVersion(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered (external)", name);
}
}
Logger.info(
this, "Registered %d EO sources from %s to %s, included %s, excluded %s",
Expand Down
25 changes: 23 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,26 @@ abstract class SafeMojo extends AbstractMojo {
@Parameter(property = "eo.cache")
protected Path cache = Paths.get(System.getProperty("user.home")).resolve(".eo");

/**
* Used for object versioning implementation.
* If set to TRUE, external tojos are used instead of foreign ones and all
* inherited Mojos behave a bit differently.
* @todo #1602:30min Remove the flag when objection versioned is
* implemented. The variable is used for implementation of object
* versioning. It allows to use external tojos instead of foreign in Mojos.
* for the test purposes. When object versioning is implemented there
* will be no need for that variable
* @checkstyle VisibilityModifierCheck (10 lines)
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
* @checkstyle MemberNameCheck (10 lines)
*/
@Parameter(property = "eo.withVersions", defaultValue = "false")
protected boolean withVersions;

/**
* Cached tojos.
* @checkstyle VisibilityModifierCheck (5 lines)
*/
protected final ForeignTojos tojos = new ForeignTojos(
private final ForeignTojos tojos = new ForeignTojos(
() -> Catalogs.INSTANCE.make(this.foreign.toPath(), this.foreignFormat),
() -> this.scope
);
Expand Down Expand Up @@ -318,7 +333,13 @@ public final void execute() throws MojoFailureException, MojoExecutionException
* @checkstyle AnonInnerLengthCheck (100 lines)
*/
protected final ForeignTojos scopedTojos() {
return this.tojos;
final ForeignTojos tjs;
if (this.external != null && this.withVersions) {
tjs = this.externalTojos;
} else {
tjs = this.tojos;
}
return tjs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,72 @@ void discoversForDifferentScopes(@TempDir final Path tmp) throws IOException {
Matchers.is(true)
);
}

@Test
void discoversWithVersions(@TempDir final Path tmp) throws IOException {
final FakeMaven maven = new FakeMaven(tmp)
.with("withVersions", true)
.withProgram(
"+alias org.eolang.txt.sprintf\n",
"[] > main",
" seq > @",
" QQ.io.stdout|0.29.1",
" sprintf|0.28.5",
" \"Hello world\"",
" TRUE"
)
.execute(new FakeMaven.Discover());
final String sprintf = "org.eolang.txt.sprintf|0.28.5";
final String stdout = "org.eolang.io.stdout|0.29.1";
MatcherAssert.assertThat(
String.format(
"External tojos have to contain %s object after discovering, but they don't",
sprintf
),
maven.externalTojos().contains(sprintf),
Matchers.is(true)
);
MatcherAssert.assertThat(
String.format(
"External tojos should not contain %s object after discovering, but they did",
stdout
),
maven.externalTojos().contains(stdout),
Matchers.is(false)
);
}

@Test
void doesNotDiscoverWithVersions(@TempDir final Path tmp) throws IOException {
final FakeMaven maven = new FakeMaven(tmp)
.with("withVersions", false)
.withProgram(
"+alias org.eolang.txt.sprintf\n",
"[] > main",
" seq > @",
" QQ.io.stdout|0.29.1",
" sprintf|0.28.5",
" \"Hello world\"",
" TRUE"
)
.execute(new FakeMaven.Discover());
final String sprintf = "org.eolang.txt.sprintf|0.28.5";
final String stdout = "org.eolang.io.stdout|0.29.1";
MatcherAssert.assertThat(
String.format(
"External tojos should not have contained %s object after discovering, but they did",
sprintf
),
maven.externalTojos().contains(sprintf),
Matchers.is(false)
);
MatcherAssert.assertThat(
String.format(
"External tojos should not have contained %s object after discovering, but they did",
stdout
),
maven.externalTojos().contains(stdout),
Matchers.is(false)
);
}
}
5 changes: 3 additions & 2 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("transpiledFormat", "csv");
this.params.putIfAbsent("skipZeroVersions", true);
this.params.putIfAbsent("discoverSelf", false);
this.params.putIfAbsent("versioned", false);
this.params.putIfAbsent("withVersions", false);
this.params.putIfAbsent("ignoreVersionConflict", false);
this.params.putIfAbsent("ignoreTransitive", true);
this.params.putIfAbsent("central", new DummyCentral());
Expand Down Expand Up @@ -339,12 +339,13 @@ FakeMaven withProgram(final Path path) throws IOException {
}

/**
* Specify hash for all foreign tojos.
* Specify hash for all foreign and external tojos.
* @param hash Commit hash
* @return The same maven instance.
*/
FakeMaven allTojosWithHash(final CommitHash hash) {
this.foreignTojos().all().forEach(tojo -> tojo.withHash(hash));
this.externalTojos().all().forEach(tojo -> tojo.withHash(hash));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.NoSuchElementException;
import org.cactoos.io.ResourceOf;
import org.eolang.maven.util.Home;
import org.hamcrest.MatcherAssert;
Expand All @@ -40,6 +41,15 @@
* @since 0.11
*/
final class RegisterMojoTest {
/**
* Parameter for source directory.
*/
private static final String PARAM = "sourcesDir";

/**
* Source directory.
*/
private static final String SOURCES = "src/eo";

@Test
void registersOkNames(@TempDir final Path temp) throws IOException {
Expand All @@ -48,7 +58,7 @@ void registersOkNames(@TempDir final Path temp) throws IOException {
Paths.get("src/eo/org/eolang/maven/abc-def.eo")
);
final FakeMaven maven = new FakeMaven(temp)
.with("sourcesDir", temp.resolve("src/eo").toFile())
.with(RegisterMojoTest.PARAM, temp.resolve(RegisterMojoTest.SOURCES).toFile())
.execute(new FakeMaven.Register());
MatcherAssert.assertThat(
maven.foreign().getById("org.eolang.maven.abc-def").exists("id"),
Expand All @@ -66,7 +76,7 @@ void failsWithDotNames(@TempDir final Path temp) throws IOException {
IllegalStateException.class,
() -> {
new FakeMaven(temp)
.with("sourcesDir", temp.resolve("src/eo").toFile())
.with(RegisterMojoTest.PARAM, temp.resolve(RegisterMojoTest.SOURCES).toFile())
.execute(new FakeMaven.Register());
}
);
Expand All @@ -83,7 +93,7 @@ void doesNotFailWhenNoStrictNames(@TempDir final Path temp) throws IOException {
Paths.get("src/eo/org/eolang/maven/.abc.eo")
);
final FakeMaven maven = new FakeMaven(temp)
.with("sourcesDir", temp.resolve("src/eo").toFile())
.with(RegisterMojoTest.PARAM, temp.resolve(RegisterMojoTest.SOURCES).toFile())
.with("strictFileNames", false)
.execute(new FakeMaven.Register());
MatcherAssert.assertThat(
Expand All @@ -99,25 +109,44 @@ void registersInExternal(@TempDir final Path temp) throws IOException {
Paths.get("src/eo/org/eolang/maven/foo.eo")
);
final String name = "org.eolang.maven.foo";
final String source = "src/eo";
final FakeMaven maven = new FakeMaven(temp)
.with("sourcesDir", temp.resolve(source).toFile())
.with(RegisterMojoTest.PARAM, temp.resolve(RegisterMojoTest.SOURCES).toFile())
.with("withVersions", true)
.execute(new FakeMaven.Register());
MatcherAssert.assertThat(
String.format(
"Source object %s placed in %s should have been registered in external tojos but it didn't",
name,
source
RegisterMojoTest.SOURCES
),
maven.external()
.getById(name)
.exists("id"),
Matchers.is(true)
);
MatcherAssert.assertThat(
"External and foreign tojos should have the same status after registering because of identical behaviour at the step but they didn't",
"External and foreign tojos should not have the same status after registering because external should replace foreign but they didn't",
maven.foreignTojos().status(),
Matchers.equalTo(maven.externalTojos().status())
Matchers.not(
Matchers.equalTo(
maven.externalTojos().status()
)
)
);
}

@Test
void doesNotRegisterInExternal(@TempDir final Path temp) throws IOException {
new Home(temp).save(
new ResourceOf("org/eolang/maven/file-name/abc-def.eo"),
Paths.get("src/eo/org/eolang/maven/foo.eo")
);
final FakeMaven maven = new FakeMaven(temp)
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
.with(RegisterMojoTest.PARAM, temp.resolve(RegisterMojoTest.SOURCES).toFile())
.with("withVersions", false);
Assertions.assertThrows(
NoSuchElementException.class,
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
() -> maven.external().getById("org.eolang.maven.foo")
);
}

Expand Down
Loading