Skip to content

Commit 28aadad

Browse files
authored
Merge pull request #471 from eed3si9n/wip/launchertest
Fix SshAgentSessionFactory
2 parents 5b5f2d8 + 6ea6554 commit 28aadad

File tree

8 files changed

+80
-31
lines changed

8 files changed

+80
-31
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- stage: test
1515
env: SBT_VERSION=1.2.8
1616
jdk: openjdk8
17-
script: sbt "-no-colors" "^^ $SBT_VERSION" ";publishLocal;scalafmtCheckAll;plugin/scripted;scaffold/scripted;lib/test;app/compile"
17+
script: sbt "-no-colors" "^^ $SBT_VERSION" ";publishLocal;scalafmtCheckAll;launcher/test;plugin/scripted;scaffold/scripted;lib/test;app/compile"
1818
- name: "Scala 2.13"
1919
script:
2020
- sbt "++ 2.13.2" app/test lib/test

build.sbt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Dependencies._
22
import CrossVersion.partialVersion
33

4-
val g8version = "0.13.0-SNAPSHOT"
4+
val g8version = "0.13.1-SNAPSHOT"
55

66
val javaVmArgs: List[String] = {
77
import scala.collection.JavaConverters._
@@ -142,6 +142,7 @@ lazy val gitsupport = (project in file("cli-git"))
142142
jsch,
143143
jschSshAgent,
144144
jschConnectorFactory,
145+
jgitJsch,
145146
commonsIo,
146147
scalatest % Test,
147148
scalamock % Test
@@ -187,8 +188,15 @@ lazy val launcher = (project in file("launcher"))
187188
description := "Command line tool to apply templates defined on GitHub",
188189
name := "giter8-launcher",
189190
crossScalaVersions := List(scala212, scala213),
190-
libraryDependencies += coursier,
191-
run / fork := true
191+
libraryDependencies ++= Seq(
192+
coursier,
193+
verify % Test,
194+
sbtIo % Test
195+
),
196+
testFrameworks += new TestFramework("verify.runner.Framework"),
197+
run / fork := true,
198+
Test / fork := true,
199+
Test / javaOptions ++= Seq(s"""-DG8_HOME=${target.value / "home"}""")
192200
// assemblyMergeStrategy in assembly := {
193201
// case "plugin.properties" => MergeStrategy.concat
194202
// case "module-info.class" => MergeStrategy.discard

cli-git/src/main/scala/ConsoleCredentialsProvider.scala

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,29 @@ object ConsoleCredentialsProvider extends CredentialsProvider {
2626
def supports(items: CredentialItem*) = true
2727

2828
def get(uri: URIish, items: CredentialItem*): Boolean = {
29-
items foreach {
30-
case i: CredentialItem.Username =>
31-
val username = System.console.readLine("%s: ", i.getPromptText)
32-
i.setValue(username)
33-
34-
case i: CredentialItem.Password =>
35-
val password = System.console.readPassword("%s: ", i.getPromptText)
36-
i.setValueNoCopy(password)
37-
38-
case i: CredentialItem.InformationalMessage =>
39-
System.console.printf("%s\n", i.getPromptText)
40-
41-
case i: CredentialItem.YesNoType =>
42-
i.setValue(askYesNo(i.getPromptText))
43-
44-
case i: CredentialItem.StringType if uri.getScheme == "ssh" =>
45-
val password = String.valueOf(System.console.readPassword("%s: ", i.getPromptText))
46-
i.setValue(password)
29+
if (System.console == null) false
30+
else {
31+
items foreach {
32+
case i: CredentialItem.Username =>
33+
val username = System.console.readLine("%s: ", i.getPromptText)
34+
i.setValue(username)
35+
36+
case i: CredentialItem.Password =>
37+
val password = System.console.readPassword("%s: ", i.getPromptText)
38+
i.setValueNoCopy(password)
39+
40+
case i: CredentialItem.InformationalMessage =>
41+
System.console.printf("%s\n", i.getPromptText)
42+
43+
case i: CredentialItem.YesNoType =>
44+
i.setValue(askYesNo(i.getPromptText))
45+
46+
case i: CredentialItem.StringType if uri.getScheme == "ssh" =>
47+
val password = String.valueOf(System.console.readPassword("%s: ", i.getPromptText))
48+
i.setValue(password)
49+
}
50+
true
4751
}
48-
true
4952
}
5053

5154
@scala.annotation.tailrec

cli-git/src/main/scala/GitInteractor.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.io.File
2121

2222
import giter8.GitInteractor.TransportError
2323
import org.eclipse.jgit.api.errors.TransportException
24-
import org.eclipse.jgit.transport.{CredentialsProvider, SshSessionFactory}
24+
import org.eclipse.jgit.transport.{CredentialsProvider, SshSessionFactory, SshTransport}
2525
import org.eclipse.jgit.api.{Git => JGit}
2626

2727
import scala.util.{Failure, Success, Try}
@@ -42,14 +42,17 @@ object GitInteractor {
4242

4343
class JGitInteractor(knownHosts: Option[String]) extends GitInteractor {
4444
CredentialsProvider.setDefault(ConsoleCredentialsProvider)
45-
SshSessionFactory.setInstance(new SshAgentSessionFactory(knownHosts))
4645

4746
override def cloneRepository(url: String, dest: File): Try[Unit] = Try {
4847
JGit
4948
.cloneRepository()
5049
.setURI(url)
5150
.setDirectory(dest)
5251
.setCredentialsProvider(ConsoleCredentialsProvider)
52+
.setTransportConfigCallback({
53+
case sshTransport: SshTransport => sshTransport.setSshSessionFactory(new SshAgentSessionFactory(knownHosts))
54+
case x => x
55+
})
5356
.call()
5457
.close()
5558
}

launcher/src/main/scala/LauncherMain.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.net.URLClassLoader
44
import java.io.{BufferedInputStream, File, FileInputStream}
55
import java.util.Properties
66
import java.lang.reflect.InvocationTargetException
7-
import java.nio.file.Files
7+
import java.nio.file.{Files, StandardCopyOption}
88

99
object LauncherMain extends Runner with App {
1010
java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.SEVERE)
@@ -35,7 +35,7 @@ class LauncherProcessor extends Processor {
3535
(if (forceOverwrite) Vector("-f") else Vector()) ++
3636
(outputDirectory match { case Some(out) => Vector("-o", out.toString); case _ => Vector() })
3737
val giter8Files = giter8Artifacts(g8v)
38-
virtuallyRun(giter8Files, virtualArgument)
38+
virtuallyRun(giter8Files, virtualArgument, workingDirectory)
3939
Right("")
4040
}
4141

@@ -67,7 +67,7 @@ class LauncherProcessor extends Processor {
6767
}
6868
downloadedJars map { downloaded =>
6969
val t = bootDir / downloaded.getName
70-
Files.copy(downloaded.toPath, t.toPath).toFile
70+
Files.copy(downloaded.toPath, t.toPath, StandardCopyOption.REPLACE_EXISTING).toFile
7171
}
7272
}
7373
// push launcher JAR to the end of classpath to avoid Scala version clash
@@ -83,9 +83,9 @@ class LauncherProcessor extends Processor {
8383
}
8484

8585
// uses classloader trick to run
86-
def virtuallyRun(files: Seq[File], args: Seq[String]): Unit = {
86+
def virtuallyRun(files: Seq[File], args: Seq[String], workingDirectory: File): Unit = {
8787
val cl = new URLClassLoader(files.map(_.toURL).toArray, null)
88-
call("giter8.Giter8", "run", cl)(classOf[Array[String]])(args.toArray)
88+
call("giter8.Giter8", "run", cl)(classOf[Array[String]], classOf[File])(args.toArray, workingDirectory)
8989
}
9090

9191
def giter8Version(templateDir: File): Option[String] = {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package testpkg
2+
3+
import giter8._
4+
import verify._
5+
import java.io.File
6+
import sbt.io.IO
7+
8+
object LauncherTest extends BasicTestSuite {
9+
lazy val launcher = new Runner {
10+
def run(args: Array[String], workingDirectory: File): Int = {
11+
run(args, workingDirectory, new LauncherProcessor)
12+
}
13+
}
14+
implicit private class RichFile(file: File) {
15+
def /(child: String): File = new File(file, child)
16+
}
17+
18+
test("runs scala/scala-seed.g8") {
19+
IO.withTemporaryDirectory { dir =>
20+
launcher.run(Array("scala/scala-seed.g8", "--name=hello"), dir)
21+
assert((dir / "hello" / "build.sbt").exists)
22+
}
23+
}
24+
25+
/*
26+
test("runs git@github.com:scala/scala-seed.g8.git") {
27+
IO.withTemporaryDirectory { dir =>
28+
launcher.run(Array("git@github.com:scala/scala-seed.g8.git", "--name=hello"), dir)
29+
assert((dir / "hello" / "build.sbt").exists)
30+
}
31+
}
32+
*/
33+
}

project/Dependencies.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ object Dependencies {
1414
ExclusionRule("com.sun.jdmk", "jmxtools"),
1515
ExclusionRule("com.sun.jmx", "jmxri")
1616
)
17+
val jgitJsch = "org.eclipse.jgit" % "org.eclipse.jgit.ssh.jsch" % "5.8.0.202006091008-r"
1718
val jsch = "com.jcraft" % "jsch.agentproxy.jsch" % "0.0.9"
1819
val jschSshAgent = "com.jcraft" % "jsch.agentproxy.sshagent" % "0.0.9"
1920
val jschConnectorFactory = "com.jcraft" % "jsch.agentproxy.connector-factory" % "0.0.9"
2021
val scopt = "com.github.scopt" %% "scopt" % "3.7.1"
2122
val scalacheck = "org.scalacheck" %% "scalacheck" % "1.14.3"
2223
val scalatest = "org.scalatest" %% "scalatest" % "3.2.0"
2324
val scalamock = "org.scalamock" %% "scalamock" % "4.4.0"
25+
val verify = "com.eed3si9n.verify" %% "verify" % "0.2.0"
2426
val sbtIo = "org.scala-sbt" %% "io" % "1.3.4"
2527
val scala212 = "2.12.11"
2628
val scala213 = "2.13.2"

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.3.10
1+
sbt.version=1.2.8

0 commit comments

Comments
 (0)