Skip to content

Commit

Permalink
Merge branch 'smithy-lang:main' into add-copying-message
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema authored Jul 28, 2023
2 parents 97bfe93 + 1f80a10 commit cfeb72b
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Smithy Changelog

## 1.35.0 (2023-07-24)
## 1.35.0 (2023-07-27)

### Features
* Enabled support for SNAPSHOT dependencies ([#1853](https://github.com/smithy-lang/smithy/pull/1853), [#1857](https://github.com/smithy-lang/smithy/pull/1857))
* Enabled support for SNAPSHOT dependencies ([#1853](https://github.com/smithy-lang/smithy/pull/1853), [#1857](https://github.com/smithy-lang/smithy/pull/1857), [#1884](https://github.com/smithy-lang/smithy/pull/1884))
* Enabled default mode for `smithy diff` rather than failing when not set ([#1856](https://github.com/smithy-lang/smithy/pull/1856))
* Added warning to mis-cased standard HTTP verbs ([#1862](https://github.com/smithy-lang/smithy/pull/1862))
* Relaxed type constraints for `pageSize` property of the `@paginated` trait ([#1866](https://github.com/smithy-lang/smithy/pull/1866))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public final class MavenRepository implements ToSmithyBuilder<MavenRepository> {

private final String url;
private final String httpCredentials;
private final String id;

public MavenRepository(Builder builder) {
this.url = SmithyBuilder.requiredState("url", builder.url);
this.httpCredentials = builder.httpCredentials;
this.id = builder.id;
}

public static Builder builder() {
Expand All @@ -41,26 +43,31 @@ public static MavenRepository fromNode(Node node) {
node.expectObjectNode()
.warnIfAdditionalProperties(Arrays.asList("url", "httpCredentials"))
.expectStringMember("url", builder::url)
.getStringMember("httpCredentials", builder::httpCredentials);
.getStringMember("httpCredentials", builder::httpCredentials)
.getStringMember("id", builder::id);
return builder.build();
}

public String getUrl() {
return url;
}

public Optional<String> getId() {
return Optional.ofNullable(id);
}

public Optional<String> getHttpCredentials() {
return Optional.ofNullable(httpCredentials);
}

@Override
public Builder toBuilder() {
return builder().url(url).httpCredentials(httpCredentials);
return builder().id(id).url(url).httpCredentials(httpCredentials);
}

@Override
public int hashCode() {
return Objects.hash(url, httpCredentials);
return Objects.hash(id, url, httpCredentials);
}

@Override
Expand All @@ -71,12 +78,16 @@ public boolean equals(Object o) {
return false;
}
MavenRepository mavenRepo = (MavenRepository) o;
return Objects.equals(url, mavenRepo.url) && Objects.equals(httpCredentials, mavenRepo.httpCredentials);
return
Objects.equals(id, mavenRepo.id)
&& Objects.equals(url, mavenRepo.url)
&& Objects.equals(httpCredentials, mavenRepo.httpCredentials);
}

public static final class Builder implements SmithyBuilder<MavenRepository> {
private String url;
private String httpCredentials;
private String id;

private Builder() {}

Expand All @@ -90,6 +101,11 @@ public Builder url(String url) {
return this;
}

public Builder id(String id) {
this.id = id;
return this;
}

public Builder httpCredentials(String httpCredentials) {
this.httpCredentials = httpCredentials;
if (httpCredentials != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,30 @@ public void withListArg() {
});
}

@Test
public void outputDirectoryAlreadyExists() {
IntegUtils.withProject(PROJECT_NAME, templatesDir -> {
setupTemplatesDirectory(templatesDir);

IntegUtils.withTempDir("existingOutputDir", dir -> {
Path existingPath = null;
RunResult result;
try {
existingPath = Files.createDirectory(dir.resolve("quickstart-cli"));
result = IntegUtils.run(
dir, ListUtils.of("init", "-t", "quickstart-cli", "-u", templatesDir.toString()));
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IoUtils.rmdir(existingPath);
}

assertThat(result.getOutput(), containsString("Output directory `quickstart-cli` already exists."));
assertThat(result.getExitCode(), is(1));
});
});
}

private static void run(List<String> args, Path root) {
StringBuilder output = new StringBuilder();
int result = IoUtils.runCommand(args, root, output, Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package software.amazon.smithy.cli;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import software.amazon.smithy.utils.ListUtils;

import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.containsString;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;

public class MavenResolverMultipleReposTest {

@Test
public void multipleRepositoriesOnSameHostName() {
ClientAndServer mockServer = null;
try {
mockServer = startClientAndServer(1234);

// artifact
mockArtifactAndSha(
mockServer,
"/artifactory-2/com/example/artifact/1.0.0-SNAPSHOT/artifact-1.0.0-20230724.184336-2.jar",
"FAKE JAR CONTENT"
);

mockArtifactAndSha(
mockServer,
"/artifactory-2/com/example/artifact/1.0.0-SNAPSHOT/artifact-1.0.0-20230724.184336-1.pom",
pomFileContents()
);

mockArtifactAndSha(
mockServer,
"/artifactory-2/com/example/artifact/1.0.0-SNAPSHOT/maven-metadata.xml",
metadataFileContents()
);

// dependency
mockArtifactAndSha(
mockServer,
"/artifactory-1/com/example/dependency/1.0.0/dependency-1.0.0.jar",
"ANOTHER FAKE JAR"
);

mockNotFound(mockServer, "/artifactory-1/.*");
mockNotFound(mockServer, "/artifactory-2/.*");

IntegUtils.runWithEmptyCache("maven-multiple-repos", ListUtils.of("validate", "--debug"),
Collections.emptyMap(), result -> {
assertThat(result.getExitCode(), equalTo(0));
assertThat(result.getOutput(), containsString("software.amazon.smithy.cli.dependencies.DependencyResolver - Resolved Maven dependencies: [com.example:artifact:jar:1.0.0-20230724.184336-2"));
});
} finally {
if(mockServer!=null) {
mockServer.stop();
}
}
}

private void mockArtifactAndSha(ClientAndServer mockServer, String path, String contents) {
mockSuccess(
mockServer,
path,
contents
);

mockSuccess(
mockServer,
path + ".sha1",
DigestUtils.sha1Hex(contents)
);
}

private String metadataFileContents() {
return
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<metadata modelVersion=\"1.1.0\">\n" +
" <groupId>com.example</groupId>\n" +
" <artifactId>artifact</artifactId>\n" +
" <version>1.0.0-SNAPSHOT</version>\n" +
" <versioning>\n" +
" <snapshot>\n" +
" <timestamp>20230724.184336</timestamp>\n" +
" <buildNumber>1</buildNumber>\n" +
" </snapshot>\n" +
" <lastUpdated>20230724184336</lastUpdated>\n" +
" <snapshotVersions>\n" +
" <snapshotVersion>\n" +
" <extension>jar</extension>\n" +
" <value>1.0.0-20230724.184336-2</value>\n" +
" <updated>20230724184336</updated>\n" +
" </snapshotVersion>\n" +
" <snapshotVersion>\n" +
" <extension>pom</extension>\n" +
" <value>1.0.0-20230724.184336-1</value>\n" +
" <updated>20230724184336</updated>\n" +
" </snapshotVersion>\n" +
" </snapshotVersions>\n" +
" </versioning>\n" +
"</metadata>\n";
}

private String pomFileContents() {
return
"<?xml version='1.0' encoding='UTF-8'?>\n" +
"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\">\n" +
" <modelVersion>4.0.0</modelVersion>\n" +
" <groupId>com.example</groupId>\n" +
" <artifactId>artifact</artifactId>\n" +
" <packaging>jar</packaging>\n" +
" <description>artifact</description>\n" +
" <version>1.0.0-SNAPSHOT</version>\n" +
" <name>artifact</name>\n" +
" <organization>\n" +
" <name>com.example</name>\n" +
" </organization>\n" +
" <properties>\n" +
" <info.versionScheme>early-semver</info.versionScheme>\n" +
" </properties>\n" +
" <dependencies>\n" +
" <dependency>\n" +
" <groupId>com.example</groupId>\n" +
" <artifactId>dependency</artifactId>\n" +
" <version>1.0.0</version>\n" +
" </dependency>\n" +
" </dependencies>\n" +
"</project>";
}

private static void mockNotFound(ClientAndServer mockServer, String path) {
mockServer.when(
HttpRequest
.request()
.withMethod("GET")
.withPath(path)
).respond(HttpResponse.notFoundResponse());
}

private static void mockSuccess(ClientAndServer mockServer, String path, String responseBody) {
mockServer.when(
HttpRequest
.request()
.withMethod("GET")
.withPath(path)
).respond(
HttpResponse
.response()
.withStatusCode(200)
.withBody(responseBody)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1.0",
"maven": {
"repositories": [
{
// Use HTTP instead of HTTPS because we're running a mock server during tests
"id": "Artifactory 1",
"url": "http://localhost:1234/artifactory-1"
},
{
// Use HTTP instead of HTTPS because we're running a mock server during tests
"id": "Artifactory 2",
"url": "http://localhost:1234/artifactory-2"
}
],
"dependencies": [
"com.example:artifact:1.0.0-SNAPSHOT"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem
throw new IllegalArgumentException("Please specify a template name using `--template` or `-t`");
}

// Use templateName if directory is not specified
if (directory == null) {
directory = template;
}
final Path dest = Paths.get(directory);
if (Files.exists(dest)) {
throw new CliError("Output directory `" + directory + "` already exists.");
}

ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode);
if (!templatesNode.containsMember(template)) {
throw new IllegalArgumentException(String.format(
Expand All @@ -186,12 +195,8 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem
exec(ListUtils.of("git", "checkout"), temp);
}

// Use templateName if directory is not specified
if (directory == null) {
directory = template;
}

final Path dest = Paths.get(directory);

IoUtils.copyDir(Paths.get(temp.toString(), templatePath), dest);
copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template, env);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable except
public void addRepository(MavenRepository repository) {
try {
URI uri = new URI(repository.getUrl());
String name = uri.getHost();
int repositoryIndex = remoteRepositories.size() + 1;
String id = repository.getId().orElseGet(() -> repositoryIndex + "|" + uri.getHost());
String userInfo = uri.getUserInfo();
RemoteRepository.Builder builder = new RemoteRepository.Builder(name, "default", repository.getUrl());
RemoteRepository.Builder builder = new RemoteRepository.Builder(id, "default", repository.getUrl());
if (userInfo != null) {
LOGGER.finest(() -> "Setting username and password for " + name + " using URI authority");
LOGGER.finest(() -> "Setting username and password for " + id + " using URI authority");
addUserInfoAuth(uri, userInfo, builder);
}
repository.getHttpCredentials().ifPresent(credentials -> addUserInfoAuth(uri, credentials, builder));
Expand Down

0 comments on commit cfeb72b

Please sign in to comment.