From 9e8f766b65589505f653febc2b32d6482d645dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Sun, 15 Sep 2019 08:57:52 +0200 Subject: [PATCH] Import example handout --- .vscode/settings.json | 8 + build.sbt | 9 + grading-tests.jar | Bin 0 -> 5072 bytes project/MOOCSettings.scala | 23 ++ project/StudentTasks.scala | 323 ++++++++++++++++++++++++ project/build.properties | 1 + project/buildSettings.sbt | 8 + project/plugins.sbt | 2 + src/main/scala/example/Lists.scala | 41 +++ src/test/scala/example/ListsSuite.scala | 99 ++++++++ student.sbt | 9 + 11 files changed, 523 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 build.sbt create mode 100644 grading-tests.jar create mode 100644 project/MOOCSettings.scala create mode 100644 project/StudentTasks.scala create mode 100644 project/build.properties create mode 100644 project/buildSettings.sbt create mode 100644 project/plugins.sbt create mode 100644 src/main/scala/example/Lists.scala create mode 100644 src/test/scala/example/ListsSuite.scala create mode 100644 student.sbt diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a35362b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "dotty": { + "trace": { + "remoteTracingUrl": "wss://lamppc36.epfl.ch/dotty-remote-tracer/upload/lsp.log", + "server": { "format": "JSON", "verbosity": "verbose" } + } + } +} diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..7ff99b8 --- /dev/null +++ b/build.sbt @@ -0,0 +1,9 @@ +course := "progfun1" +assignment := "example" +name := course.value + "-" + assignment.value +testSuite := "example.ListsSuite" +scalaVersion := "0.19.0-bin-20190917-d821081-NIGHTLY" +scalacOptions ++= Seq("-language:implicitConversions", "-deprecation") +libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test + +testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s") diff --git a/grading-tests.jar b/grading-tests.jar new file mode 100644 index 0000000000000000000000000000000000000000..ffa1d53f6abc827ba6aed454fca3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2xY3(Pdz3y&rtjTPi`*x=vjQ)z@;jnbfrJZl9;xt4PbiJ zXZBrh+O7#hsC&}=0BcO2Uq>q@*(~!LIE#+r&FoILx>L6tgby09F6FrjMb}F6#duv>^;7os&iT^Q3=|_)V~5IwSWf}2 z=)`d^s*exjT6V%b%=%FOBB!dsj%*3nylLId27`1PgElVMmsqkrU#m$-yOF3}K;wrjIPWM!mD5+3sXjNAEi0FRE zfV16;2uA+GdkK5|?(A8r0Cf~t_yV(k&o6vv+;^eMmx6kau4de#N$0D2fViEn;3a zQimUZ78OOTN9+_bknbg57yfg3eXyRUyFHGJru*YvL_Sg1w4()+=bEao8>!g9h{fX{ zkymVPUBOY`i2B;%ZWxR>jN+v~7@iceJi?aTPQAo2LRXfvX^4~KdUT*4D_>D8-=yKk z8`Z*wuVDdHrU4=9flW9HN84r1nl8~L2@Bhi9#Dev*JvX(c7ar}$E%`=8`7OcpukK$}x(WZS%74!?T@5VkKk+bsAKc$K Lto8R)1OWaEz-m19 literal 0 HcmV?d00001 diff --git a/project/MOOCSettings.scala b/project/MOOCSettings.scala new file mode 100644 index 0000000..80e34b8 --- /dev/null +++ b/project/MOOCSettings.scala @@ -0,0 +1,23 @@ +package ch.epfl.lamp + +import sbt._ +import sbt.Keys._ + +/** + * Settings shared by all assignments, reused in various tasks. + */ +object MOOCSettings extends AutoPlugin { + + object autoImport { + val course = SettingKey[String]("course") + val assignment = SettingKey[String]("assignment") + val testSuite = SettingKey[String]("testSuite") + val options = SettingKey[Map[String, Map[String, String]]]("options") + } + + override def trigger = allRequirements + + override val projectSettings: Seq[Def.Setting[_]] = Seq( + parallelExecution in Test := false + ) +} diff --git a/project/StudentTasks.scala b/project/StudentTasks.scala new file mode 100644 index 0000000..bec5cec --- /dev/null +++ b/project/StudentTasks.scala @@ -0,0 +1,323 @@ +package ch.epfl.lamp + +import sbt._ +import Keys._ + +// import scalaj.http._ +import java.io.{File, FileInputStream, IOException} +import java.nio.file.FileSystems +import org.apache.commons.codec.binary.Base64 +// import play.api.libs.json.{Json, JsObject, JsPath} +import scala.util.{Failure, Success, Try} + +import MOOCSettings.autoImport._ + +case class AssignmentInfo( + key: String, + itemId: String, + premiumItemId: Option[String], + partId: String +) + +/** + * Provides tasks for submitting the assignment + */ +object StudentTasks extends AutoPlugin { + + object autoImport { + val assignmentInfo = SettingKey[AssignmentInfo]("assignmentInfo") + + val packageSourcesOnly = TaskKey[File]("packageSourcesOnly", "Package the sources of the project") + val packageBinWithoutResources = TaskKey[File]("packageBinWithoutResources", "Like packageBin, but without the resources") + val packageSubmissionZip = TaskKey[File]("packageSubmissionZip") + val packageSubmission = inputKey[Unit]("package solution as an archive file") + val runGradingTests = taskKey[Unit]("run black-box tests used for final grading") + } + + + import autoImport._ + + override lazy val projectSettings = Seq( + packageSubmissionSetting, + // submitSetting, // FIXME: restore assignmentInfo setting on assignments + runGradingTestsSettings, + + fork := true, + connectInput in run := true, + outputStrategy := Some(StdoutOutput) + ) ++ packageSubmissionZipSettings + + lazy val runGradingTestsSettings = runGradingTests := { + val testSuiteJar = "grading-tests.jar" + if (!new File(testSuiteJar).exists) { + throw new MessageOnlyException(s"Could not find tests JarFile: $testSuiteJar") + } + + val classPath = s"${(Test / dependencyClasspath).value.map(_.data).mkString(":")}:$testSuiteJar" + val junitProcess = + Fork.java.fork( + ForkOptions(), + "-cp" :: classPath :: + "org.junit.runner.JUnitCore" :: + (Test / testSuite).value :: + Nil + ) + + // Wait for tests to complete. + junitProcess.exitValue() + } + + + /** ********************************************************** + * SUBMITTING A SOLUTION TO COURSERA + */ + + val packageSubmissionZipSettings = Seq( + packageSubmissionZip := { + val submission = crossTarget.value / "submission.zip" + val sources = (packageSourcesOnly in Compile).value + val binaries = (packageBinWithoutResources in Compile).value + IO.zip(Seq(sources -> "sources.zip", binaries -> "binaries.jar"), submission) + submission + }, + artifactClassifier in packageSourcesOnly := Some("sources"), + artifact in (Compile, packageBinWithoutResources) ~= (art => art.withName(art.name + "-without-resources")) + ) ++ + inConfig(Compile)( + Defaults.packageTaskSettings(packageSourcesOnly, Defaults.sourceMappings) ++ + Defaults.packageTaskSettings(packageBinWithoutResources, Def.task { + val relativePaths = + (unmanagedResources in Compile).value.flatMap(Path.relativeTo((unmanagedResourceDirectories in Compile).value)(_)) + (mappings in (Compile, packageBin)).value.filterNot { case (_, path) => relativePaths.contains(path) } + }) + ) + + val maxSubmitFileSize = { + val mb = 1024 * 1024 + 10 * mb + } + + /** Check that the jar exists, isn't empty, isn't crazy big, and can be read + * If so, encode jar as base64 so we can send it to Coursera + */ + def prepareJar(jar: File, s: TaskStreams): String = { + val errPrefix = "Error submitting assignment jar: " + val fileLength = jar.length() + if (!jar.exists()) { + s.log.error(errPrefix + "jar archive does not exist\n" + jar.getAbsolutePath) + failSubmit() + } else if (fileLength == 0L) { + s.log.error(errPrefix + "jar archive is empty\n" + jar.getAbsolutePath) + failSubmit() + } else if (fileLength > maxSubmitFileSize) { + s.log.error(errPrefix + "jar archive is too big. Allowed size: " + + maxSubmitFileSize + " bytes, found " + fileLength + " bytes.\n" + + jar.getAbsolutePath) + failSubmit() + } else { + val bytes = new Array[Byte](fileLength.toInt) + val sizeRead = try { + val is = new FileInputStream(jar) + val read = is.read(bytes) + is.close() + read + } catch { + case ex: IOException => + s.log.error(errPrefix + "failed to read sources jar archive\n" + ex.toString) + failSubmit() + } + if (sizeRead != bytes.length) { + s.log.error(errPrefix + "failed to read the sources jar archive, size read: " + sizeRead) + failSubmit() + } else encodeBase64(bytes) + } + } + + /** Task to package solution to a given file path */ + lazy val packageSubmissionSetting = packageSubmission := { + val args: Seq[String] = Def.spaceDelimited("[path]").parsed + val s: TaskStreams = streams.value // for logging + val jar = (packageSubmissionZip in Compile).value + + val base64Jar = prepareJar(jar, s) + + val path = args.headOption.getOrElse((baseDirectory.value / "submission.jar").absolutePath) + scala.tools.nsc.io.File(path).writeAll(base64Jar) + } + +/* + /** Task to submit a solution to coursera */ + val submit = inputKey[Unit]("submit solution to Coursera") + lazy val submitSetting = submit := { + val args: Seq[String] = Def.spaceDelimited("").parsed + val s: TaskStreams = streams.value // for logging + val jar = (packageSubmissionZip in Compile).value + + val assignmentDetails = assignmentInfo.value + val assignmentKey = assignmentDetails.key + val courseName = + course.value match { + case "capstone" => "scala-capstone" + case "bigdata" => "scala-spark-big-data" + case other => other + } + + val partId = assignmentDetails.partId + val itemId = assignmentDetails.itemId + val premiumItemId = assignmentDetails.premiumItemId + + val (email, secret) = args match { + case email :: secret :: Nil => + (email, secret) + case _ => + val inputErr = + s"""|Invalid input to `submit`. The required syntax for `submit` is: + |submit + | + |The submit token is NOT YOUR LOGIN PASSWORD. + |It can be obtained from the assignment page: + |https://www.coursera.org/learn/$courseName/programming/$itemId + |${ + premiumItemId.fold("") { id => + s"""or (for premium learners): + |https://www.coursera.org/learn/$courseName/programming/$id + """.stripMargin + } + } + """.stripMargin + s.log.error(inputErr) + failSubmit() + } + + val base64Jar = prepareJar(jar, s) + val json = + s"""|{ + | "assignmentKey":"$assignmentKey", + | "submitterEmail":"$email", + | "secret":"$secret", + | "parts":{ + | "$partId":{ + | "output":"$base64Jar" + | } + | } + |}""".stripMargin + + def postSubmission[T](data: String): Try[HttpResponse[String]] = { + val http = Http("https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1") + val hs = List( + ("Cache-Control", "no-cache"), + ("Content-Type", "application/json") + ) + s.log.info("Connecting to Coursera...") + val response = Try(http.postData(data) + .headers(hs) + .option(HttpOptions.connTimeout(10000)) // scalaj default timeout is only 100ms, changing that to 10s + .asString) // kick off HTTP POST + response + } + + val connectMsg = + s"""|Attempting to submit "${assignment.value}" assignment in "$courseName" course + |Using: + |- email: $email + |- submit token: $secret""".stripMargin + s.log.info(connectMsg) + + def reportCourseraResponse(response: HttpResponse[String]): Unit = { + val code = response.code + val respBody = response.body + + /* Sample JSON response from Coursera + { + "message": "Invalid email or token.", + "details": { + "learnerMessage": "Invalid email or token." + } + } + */ + + // Success, Coursera responds with 2xx HTTP status code + if (response.is2xx) { + val successfulSubmitMsg = + s"""|Successfully connected to Coursera. (Status $code) + | + |Assignment submitted successfully! + | + |You can see how you scored by going to: + |https://www.coursera.org/learn/$courseName/programming/$itemId/ + |${ + premiumItemId.fold("") { id => + s"""or (for premium learners): + |https://www.coursera.org/learn/$courseName/programming/$id + """.stripMargin + } + } + |and clicking on "My Submission".""".stripMargin + s.log.info(successfulSubmitMsg) + } + + // Failure, Coursera responds with 4xx HTTP status code (client-side failure) + else if (response.is4xx) { + val result = Try(Json.parse(respBody)).toOption + val learnerMsg = result match { + case Some(resp: JsObject) => + (JsPath \ "details" \ "learnerMessage").read[String].reads(resp).get + case Some(x) => // shouldn't happen + "Could not parse Coursera's response:\n" + x + case None => + "Could not parse Coursera's response:\n" + respBody + } + val failedSubmitMsg = + s"""|Submission failed. + |There was something wrong while attempting to submit. + |Coursera says: + |$learnerMsg (Status $code)""".stripMargin + s.log.error(failedSubmitMsg) + } + + // Failure, Coursera responds with 5xx HTTP status code (server-side failure) + else if (response.is5xx) { + val failedSubmitMsg = + s"""|Submission failed. + |Coursera seems to be unavailable at the moment (Status $code) + |Check https://status.coursera.org/ and try again in a few minutes. + """.stripMargin + s.log.error(failedSubmitMsg) + } + + // Failure, Coursera repsonds with an unexpected status code + else { + val failedSubmitMsg = + s"""|Submission failed. + |Coursera replied with an unexpected code (Status $code) + """.stripMargin + s.log.error(failedSubmitMsg) + } + } + + // kick it all off, actually make request + postSubmission(json) match { + case Success(resp) => reportCourseraResponse(resp) + case Failure(e) => + val failedConnectMsg = + s"""|Connection to Coursera failed. + |There was something wrong while attempting to connect to Coursera. + |Check your internet connection. + |${e.toString}""".stripMargin + s.log.error(failedConnectMsg) + } + + } +*/ + + def failSubmit(): Nothing = { + sys.error("Submission failed") + } + + /** + * ***************** + * DEALING WITH JARS + */ + def encodeBase64(bytes: Array[Byte]): String = + new String(Base64.encodeBase64(bytes)) +} diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..c0bab04 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.2.8 diff --git a/project/buildSettings.sbt b/project/buildSettings.sbt new file mode 100644 index 0000000..a309025 --- /dev/null +++ b/project/buildSettings.sbt @@ -0,0 +1,8 @@ +libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test +// Used for base64 encoding +libraryDependencies += "commons-codec" % "commons-codec" % "1.10" + +// Used for Coursera submussion +// libraryDependencies += "org.scalaj" %% "scalaj-http" % "2.3.0" +// libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.9" + diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..64a2492 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,2 @@ +addSbtPlugin("io.get-coursier" % "sbt-coursier" % "2.0.0-RC3-5") +addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.3.4") diff --git a/src/main/scala/example/Lists.scala b/src/main/scala/example/Lists.scala new file mode 100644 index 0000000..861f40e --- /dev/null +++ b/src/main/scala/example/Lists.scala @@ -0,0 +1,41 @@ +package example + +object Lists { + + /** + * This method computes the sum of all elements in the list xs. There are + * multiple techniques that can be used for implementing this method, and + * you will learn during the class. + * + * For this example assignment you can use the following methods in class + * `List`: + * + * - `xs.isEmpty: Boolean` returns `true` if the list `xs` is empty + * - `xs.head: Int` returns the head element of the list `xs`. If the list + * is empty an exception is thrown + * - `xs.tail: List[Int]` returns the tail of the list `xs`, i.e. the the + * list `xs` without its `head` element + * + * ''Hint:'' instead of writing a `for` or `while` loop, think of a recursive + * solution. + * + * @param xs A list of natural numbers + * @return The sum of all elements in `xs` + */ + def sum(xs: List[Int]): Int = ??? + + /** + * This method returns the largest element in a list of integers. If the + * list `xs` is empty it throws a `java.util.NoSuchElementException`. + * + * You can use the same methods of the class `List` as mentioned above. + * + * ''Hint:'' Again, think of a recursive solution instead of using looping + * constructs. You might need to define an auxiliary method. + * + * @param xs A list of natural numbers + * @return The largest element in `xs` + * @throws java.util.NoSuchElementException if `xs` is an empty list + */ + def max(xs: List[Int]): Int = ??? +} diff --git a/src/test/scala/example/ListsSuite.scala b/src/test/scala/example/ListsSuite.scala new file mode 100644 index 0000000..ed42759 --- /dev/null +++ b/src/test/scala/example/ListsSuite.scala @@ -0,0 +1,99 @@ +package example + +import org.junit._ +import org.junit.Assert.assertEquals + +/** + * This class implements a JUnit test suite for the methods in object + * `Lists` that need to be implemented as part of this assignment. A test + * suite is simply a collection of individual tests for some specific + * component of a program. + * + * To run this test suite, start "sbt" then run the "test" command. + */ +class ListsSuite { + + /** + * Tests are written using the @Test annotation + * + * The most common way to implement a test body is using the method `assert` + * which tests that its argument evaluates to `true`. So one of the simplest + * successful tests is the following: + */ + @Test def `one plus one is two (0pts)`: Unit = + assert(1 + 1 == 2) + + @Test def `one plus one is three (0pts)?`: Unit = + assert(1 + 1 == 3) // This assertion fails! Go ahead and fix it. + + /** + * One problem with the previous (failing) test is that JUnit will + * only tell you that a test failed, but it will not tell you what was + * the reason for the failure. The output looks like this: + * + * {{{ + * [info] - one plus one is three? *** FAILED *** + * }}} + * + * This situation can be improved by using a Assert.assertEquals + * (this is only possible in JUnit). So if you + * run the next test, JUnit will show the following output: + * + * {{{ + * [info] - details why one plus one is not three *** FAILED *** + * [info] 2 did not equal 3 (ListsSuite.scala:67) + * }}} + * + * We recommend to always use the Assert.assertEquals equality operator + * when writing tests. + */ + @Test def `details why one plus one is not three (0pts)`: Unit = + Assert.assertEquals(3, 1 + 1) // Fix me, please! + + /** + * Exceptional behavior of a methods can be tested using a try/catch + * and a failed assertion. + * + * In the following example, we test the fact that the method `intNotZero` + * throws an `IllegalArgumentException` if its argument is `0`. + */ + @Test def `intNotZero throws an exception if its argument is 0`: Unit = + try + intNotZero(0) + Assert.fail("No exception has been thrown") + catch + case e: IllegalArgumentException => () + + def intNotZero(x: Int): Int = + if x == 0 then throw new IllegalArgumentException("zero is not allowed") + else x + + /** + * Now we finally write some tests for the list functions that have to be + * implemented for this assignment. We fist import all members of the + * `List` object. + */ + import Lists._ + + + /** + * We only provide two very basic tests for you. Write more tests to make + * sure your `sum` and `max` methods work as expected. + * + * In particular, write tests for corner cases: negative numbers, zeros, + * empty lists, lists with repeated elements, etc. + * + * It is allowed to have multiple `assert` statements inside one test, + * however it is recommended to write an individual `test` statement for + * every tested aspect of a method. + */ + @Test def `sum of a few numbers (10pts)`: Unit = + assert(sum(List(1,2,0)) == 3) + + @Test def `max of a few numbers (10pts)`: Unit = + assert(max(List(3, 7, 2)) == 7) + + + + @Rule def individualTestTimeout = new org.junit.rules.Timeout(1000) +} diff --git a/student.sbt b/student.sbt new file mode 100644 index 0000000..855fa0c --- /dev/null +++ b/student.sbt @@ -0,0 +1,9 @@ +// Used for base64 encoding +libraryDependencies += "commons-codec" % "commons-codec" % "1.10" + +// Used for Coursera submussion +// libraryDependencies += "org.scalaj" %% "scalaj-http" % "2.4.2" +// libraryDependencies += "com.typesafe.play" %% "play-json" % "2.7.4" + +// Student tasks (i.e. packageSubmission) +enablePlugins(StudentTasks)