Skip to content

Commit fc6f89d

Browse files
committed
Simplify the empty YAML file detection
1 parent 9eb84d0 commit fc6f89d

3 files changed

Lines changed: 16 additions & 63 deletions

File tree

core/src/main/scala/com/gu/devenv/Config.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,15 @@ object Config {
3838
projectConfig <- json.as[ProjectConfig].toTry
3939
} yield projectConfig
4040

41-
def parseUserConfig(contents: String): Try[UserConfig] = {
42-
// Handle empty files or files with only comments/whitespace
43-
def isEmptyOrCommentsOnly(text: String): Boolean = {
44-
text.linesIterator
45-
.map(_.trim)
46-
.filter(_.nonEmpty)
47-
.forall(_.startsWith("#"))
48-
}
49-
50-
if (isEmptyOrCommentsOnly(contents)) {
41+
def parseUserConfig(contents: String): Try[UserConfig] =
42+
if (yamlIsEmpty(contents)) {
5143
scala.util.Success(UserConfig(None, None))
5244
} else {
5345
for {
5446
json <- parser.parse(contents).toTry
5547
userConfig <- json.as[UserConfig].toTry
5648
} yield userConfig
5749
}
58-
}
5950

6051
def mergeConfigs(
6152
projectConfig: ProjectConfig,
@@ -190,6 +181,15 @@ object Config {
190181
envList.map(env => env.name -> Json.fromString(env.value)): _*
191182
)
192183

184+
/** Parsing an empty YAML file throws an exception, but an empty YAML file is valid and should
185+
* result in an empty configuration. We check for empty contents here.
186+
*/
187+
private def yamlIsEmpty(text: String): Boolean =
188+
text.linesIterator
189+
.map(_.trim)
190+
.filter(_.nonEmpty)
191+
.forall(_.startsWith("#"))
192+
193193
private def applyPlugins(
194194
projectPlugins: Plugins,
195195
userPlugins: Option[Plugins]

core/src/main/scala/com/gu/devenv/models.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ case class UserConfig(
3030
plugins: Option[Plugins],
3131
dotfiles: Option[Dotfiles]
3232
)
33+
object UserConfig {
34+
val empty = UserConfig(None, None)
35+
}
3336

3437
enum ForwardPort {
3538
case SamePort(port: Int)

core/src/test/scala/com/gu/devenv/ConfigTest.scala

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ class ConfigTest
9797
val emptyConfig = ""
9898
val Success(userConfig) =
9999
Config.parseUserConfig(emptyConfig).success
100-
101-
userConfig should have(
102-
"plugins" as None,
103-
"dotfiles" as None
104-
)
100+
userConfig shouldBe UserConfig.empty
105101
}
106102

107103
"parses a user config file with only comments" in {
@@ -111,53 +107,7 @@ class ConfigTest
111107
val Success(userConfig) =
112108
Config.parseUserConfig(commentsOnlyConfig).success
113109

114-
userConfig should have(
115-
"plugins" as None,
116-
"dotfiles" as None
117-
)
118-
}
119-
120-
"parses a user config file with comments and YAML content" in {
121-
val configWithComments = """# This is a comment
122-
|plugins:
123-
| intellij:
124-
| - "com.example.plugin"
125-
| vscode:
126-
| - "example.vscode-plugin"
127-
|""".stripMargin
128-
val Success(userConfig) =
129-
Config.parseUserConfig(configWithComments).success
130-
131-
userConfig should have(
132-
"plugins" as Some(
133-
Plugins(
134-
List("com.example.plugin"),
135-
List("example.vscode-plugin")
136-
)
137-
),
138-
"dotfiles" as None
139-
)
140-
}
141-
142-
"parses a user config file with inline comments" in {
143-
val configWithInlineComments = """plugins: # User plugins
144-
| intellij:
145-
| - "com.example.plugin" # Example plugin
146-
| vscode:
147-
| - "example.vscode-plugin"
148-
|""".stripMargin
149-
val Success(userConfig) =
150-
Config.parseUserConfig(configWithInlineComments).success
151-
152-
userConfig should have(
153-
"plugins" as Some(
154-
Plugins(
155-
List("com.example.plugin"),
156-
List("example.vscode-plugin")
157-
)
158-
),
159-
"dotfiles" as None
160-
)
110+
userConfig shouldBe UserConfig.empty
161111
}
162112
}
163113

0 commit comments

Comments
 (0)