From d4766dadf234d1df356b3f33c0344f512404fb98 Mon Sep 17 00:00:00 2001 From: Lauri Lehmijoki Date: Wed, 21 Jan 2015 15:47:33 +0200 Subject: [PATCH] Show a helpful error msg if site dir does not exist --- src/main/scala/s3/website/model/Site.scala | 57 ++++++++++++++----- src/test/scala/s3/website/S3WebsiteSpec.scala | 17 +++++- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/main/scala/s3/website/model/Site.scala b/src/main/scala/s3/website/model/Site.scala index 4d080746..8152ecef 100644 --- a/src/main/scala/s3/website/model/Site.scala +++ b/src/main/scala/s3/website/model/Site.scala @@ -86,30 +86,61 @@ object Site { def loadSite(implicit yamlConfig: S3_website_yml, cliArgs: CliArgs, workingDirectory: File, logger: Logger): Either[ErrorReport, Site] = parseConfig.right.flatMap { cfg => implicit val config: Config = cfg - val errorOrSiteDir = resolveSiteDir.fold(Left(ErrorReport(noSiteFound)): Either[ErrorReport, File])(Right(_)) - errorOrSiteDir.right.map(Site(_, config)) + resolveSiteDir.right.map(Site(_, config)) } - val noSiteFound = - """|Could not find a website. - |Either use the --site=DIR command-line argument or define the location of the site in s3_website.yml. - | - |Here's an example of how you can define the site directory in s3_website.yml: - | site: dist/website""".stripMargin + def noSiteFound(explanation: String) = + s"""| + |$explanation. + |Either use the --site=DIR command-line argument or define the location of the site in s3_website.yml. + | + |Here's an example of how you can define the site directory in s3_website.yml: + | site: dist/website""".stripMargin - def resolveSiteDir(implicit yamlConfig: S3_website_yml, config: Config, cliArgs: CliArgs, workingDirectory: File): Option[File] = { + def resolveSiteDir(implicit yamlConfig: S3_website_yml, config: Config, cliArgs: CliArgs, workingDirectory: File): Either[ErrorReport, File] = { val siteFromAutoDetect = autodetectSiteDir(workingDirectory) - val siteFromCliArgs = Option(cliArgs.site).map(new File(_)) + val errOrSiteFromCliArgs: Either[ErrorReport, Option[File]] = Option(cliArgs.site) match { + case Some(siteDirFromCliArgs) => + val f = new File(siteDirFromCliArgs) + if (f.exists()) + Right(Some(f)) + else + Left(ErrorReport(noSiteFound(s"Could not find a site at $siteDirFromCliArgs. Check the --site argument."))) + case None => Right(None) + } - siteFromCliArgs orElse siteFromConfig orElse siteFromAutoDetect + val errOrAvailableSiteDirs: Either[ErrorReport, List[File]] = for { + s1 <- errOrSiteFromCliArgs.right + s2 <- siteFromConfig.right + s3 <- Right(siteFromAutoDetect).right + } yield { + (s1 :: s2 :: s3 :: Nil) collect { + case Some(file) => file + } + } + errOrAvailableSiteDirs.right.flatMap { + case mostPreferredSiteDir :: xs => Right(mostPreferredSiteDir) + case Nil => Left(ErrorReport(noSiteFound("Could not find a website."))) + } } - def siteFromConfig(implicit yamlConfig: S3_website_yml, config: Config, workingDirectory: File): Option[File] = - config + def siteFromConfig(implicit yamlConfig: S3_website_yml, config: Config, workingDirectory: File): Either[ErrorReport, Option[File]] = { + val siteConfig = config .site .map(new File(_)) .map { siteDir => if (siteDir.isAbsolute) siteDir else new File(yamlConfig.file.getParentFile, siteDir.getPath) } + + siteConfig match { + case s @ Some(siteDir) => + if (siteDir.exists()) + Right(s) + else + Left(ErrorReport(noSiteFound(s"Could not find a website. (The site setting in s3_website.yml points to a non-existing file $siteDir)"))) + case None => + Right(None) + } + } } \ No newline at end of file diff --git a/src/test/scala/s3/website/S3WebsiteSpec.scala b/src/test/scala/s3/website/S3WebsiteSpec.scala index 155c3313..2fbde5f1 100644 --- a/src/test/scala/s3/website/S3WebsiteSpec.scala +++ b/src/test/scala/s3/website/S3WebsiteSpec.scala @@ -389,11 +389,24 @@ class S3WebsiteSpec extends Specification { |ignore_on_server: | - tags/笔记/test.html """.stripMargin - push + push() noDeletesOccurred must beTrue } } + "error message" should { + "be helpful when the site directory is missing" in new BasicSetup { + config = "site: nonexisting_site_dir" + val logEntries = new mutable.MutableList[String] + push(logCapturer = Some((logEntry: String) => + logEntries += logEntry + )) + logEntries must contain( + s"Could not find a website. (The site setting in s3_website.yml points to a non-existing file $siteDirectory/nonexisting_site_dir)" + ) + } + } + "site in config" should { "let the user deploy a site from a custom location" in new CustomSiteDirectory with EmptySite with MockAWS with DefaultRunMode { config = s"site: $siteDirectory" @@ -407,7 +420,7 @@ class S3WebsiteSpec extends Specification { } "not override the --site command-line switch" in new BasicSetup { - config = s"site: dir-that-does-not-exist" + config = s"site: ${System.getProperty("java.io.tmpdir")}" setLocalFile(".vimrc") // This creates a file in the directory into which the --site CLI arg points push() sentPutObjectRequest.getKey must equalTo(".vimrc")