-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate that inline snapshot source is within source directory. (#6)
* Validate that inline snapshot source is within source directory. * Update modules/core/src/main/scala/com/siriusxm/snapshot4s/SnapshotConfigError.scala Co-authored-by: Michał Pawlik <[email protected]> * Add comments and extend Exception. --------- Co-authored-by: Michał Pawlik <[email protected]>
- Loading branch information
1 parent
3f6d95b
commit 7f0eb19
Showing
9 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
modules/core/src/main/scala/com/siriusxm/snapshot4s/SnapshotConfigError.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2024 SiriusXM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package snapshot4s | ||
|
||
private final class SnapshotConfigUnsupportedError(config: SnapshotConfig) extends Exception({ | ||
s"""Your project setup is not supported by snapshot4s. We encourage you to raise an issue at https://github.com/siriusxm/snapshot4s. | ||
|
||
We have detected the following configuration: | ||
- sourceDirectory: ${config.sourceDirectory.value} | ||
- resourceDirectory: ${config.resourceDirectory.value} | ||
- outputDirectory: ${config.outputDirectory.value} | ||
""" | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
modules/core/src/test/scala/com/siriusxm/snapshot4s/InlineSnapshotSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package snapshot4s | ||
|
||
import cats.effect.IO | ||
import weaver.* | ||
|
||
object InlineSnapshotSpec extends SimpleIOSuite { | ||
|
||
pureTest("calculates relative paths correctly") { | ||
val config = new SnapshotConfig( | ||
resourceDirectory = Path("/path/to/resources"), | ||
outputDirectory = Path("/path/to/output"), | ||
sourceDirectory = Path("/path/to/sources") | ||
) | ||
val relativePath = | ||
InlineSnapshot.relativeSourceFilePath("/path/to/sources/TestFile.scala", config) | ||
expect.eql(relativePath.value, "TestFile.scala") | ||
} | ||
|
||
test("raises errors if the source directory is calculated incorrectly") { | ||
val config = new SnapshotConfig( | ||
resourceDirectory = Path("/path/to/resources"), | ||
outputDirectory = Path("/path/to/output"), | ||
sourceDirectory = Path("/wrong/path/to/sources") | ||
) | ||
val result = | ||
IO(InlineSnapshot.relativeSourceFilePath("/path/to/sources/TestFile.scala", config)) | ||
val message = | ||
"""Your project setup is not supported by snapshot4s. We encourage you to raise an issue at https://github.com/siriusxm/snapshot4s. | ||
We have detected the following configuration: | ||
- sourceDirectory: /wrong/path/to/sources | ||
- resourceDirectory: /path/to/resources | ||
- outputDirectory: /path/to/output | ||
""" | ||
result.attempt.map { result => | ||
matches(result) { case Left(err) => | ||
expect.eql(err.getMessage, message) | ||
} | ||
} | ||
|
||
} | ||
} |