This PR contains the work done to overhaul the static asset definitions for the Website service.
To provide further details about the work:
* Website library
* Overhauled the `StateFile` enumeration to reduce the number of cases to one case per logical file name, each exposing a `fileExtensions` list.
* Wired everything into the pages with a consistent ordering convention: stylesheets load shared-first so the page sheet wins the CSS cascade; scripts load page-first with shared.js last. The error page also gained the shared stylesheet and its scripts; the index page gained its page CSS/JS and the new touch icon link.
* Used the `StaticFile` enumeration as a single source of truth for every _href_/_src_ in the `IndexPage` and the `ErrorPage` pages, eliminating hardcoded asset paths.
* Website service
* Added new assets to the Resources folder:
* `apple-touch-icon.png`
* `css/index.css`
* `js/index.js`
* `js/error.js`
* `sitemap.xml`
* Renamed existing assets within the Resources folder:
* `css/style.css` → `css/shared.css`
* `js/app.js` → `js/shared.js`
* Fixed the working-directory location for the scheme in the Xcode project.
Reviewed-on: rock-n-code/loud-amsterdam#18
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
193 lines
4.2 KiB
Swift
193 lines
4.2 KiB
Swift
import Foundation
|
|
import Testing
|
|
|
|
@testable import WebsiteLibrary
|
|
|
|
@Suite("StaticFile enumeration")
|
|
struct StaticFileTests {
|
|
|
|
// MARK: Type aliases
|
|
|
|
typealias File = StaticFile
|
|
typealias FileExtension = StaticFile.Extension
|
|
|
|
// MARK: Computed tests
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.fileExtensions
|
|
))
|
|
func `file extensions`(
|
|
for file: File,
|
|
expects extensions: [FileExtension]
|
|
) {
|
|
#expect(file.fileExtensions == extensions)
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.fileNames
|
|
))
|
|
func `file name`(
|
|
for file: File,
|
|
expects fileName: String
|
|
) {
|
|
#expect(file.fileName == fileName)
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
Self.extensions,
|
|
Self.contentTypes
|
|
))
|
|
func `content type`(
|
|
for fileExtension: FileExtension,
|
|
expects contentType: String
|
|
) {
|
|
#expect(fileExtension.contentType == contentType)
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
Self.extensions,
|
|
Self.subdirectories
|
|
))
|
|
func `subdirectory`(
|
|
for fileExtension: FileExtension,
|
|
expects subdirectory: String?
|
|
) {
|
|
#expect(fileExtension.subdirectory == subdirectory)
|
|
}
|
|
|
|
// MARK: Method tests
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.relativePaths
|
|
))
|
|
func `relative path for`(
|
|
for file: File,
|
|
expects relativePaths: [String]
|
|
) {
|
|
for (fileExtension, relativePath) in zip(file.fileExtensions, relativePaths) {
|
|
#expect(file.relativePath(for: fileExtension) == relativePath)
|
|
}
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.relativePaths
|
|
))
|
|
func `url path for`(
|
|
for file: File,
|
|
expects relativePaths: [String]
|
|
) {
|
|
for (fileExtension, relativePath) in zip(file.fileExtensions, relativePaths) {
|
|
#expect(file.urlPath(for: fileExtension) == "/\(relativePath)")
|
|
}
|
|
}
|
|
|
|
@Test(arguments: [
|
|
"",
|
|
".",
|
|
"Resources/Static"
|
|
])
|
|
func `path relative to`(
|
|
_ basePath: String
|
|
) {
|
|
for file in File.allCases {
|
|
for fileExtension in file.fileExtensions {
|
|
let pathRelativeToBasePath = file.path(
|
|
relativeTo: basePath,
|
|
for: fileExtension
|
|
)
|
|
let relativePath = file.relativePath(for: fileExtension)
|
|
|
|
if basePath.isEmpty {
|
|
#expect(pathRelativeToBasePath == relativePath)
|
|
} else {
|
|
#expect(pathRelativeToBasePath == "\(basePath)/\(relativePath)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: CaseIterable tests
|
|
|
|
@Test
|
|
func `all cases`() {
|
|
#expect(File.allCases.count == 9)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension StaticFileTests {
|
|
|
|
// MARK: Constants
|
|
|
|
static let extensions: [FileExtension] = [
|
|
.css,
|
|
.js,
|
|
.png,
|
|
.ico,
|
|
.svg,
|
|
.txt,
|
|
.webmanifest,
|
|
.xml
|
|
]
|
|
static let contentTypes: [String] = [
|
|
"text/css",
|
|
"text/javascript",
|
|
"image/png",
|
|
"image/vnd.microsoft.icon",
|
|
"image/svg+xml",
|
|
"text/plain",
|
|
"application/manifest+json",
|
|
"application/xml"
|
|
]
|
|
static let subdirectories: [String?] = [
|
|
"css",
|
|
"js",
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil
|
|
]
|
|
static let fileExtensions: [[FileExtension]] = [
|
|
[.png],
|
|
[.css, .js],
|
|
[.ico],
|
|
[.png, .svg],
|
|
[.css, .js],
|
|
[.txt],
|
|
[.css, .js],
|
|
[.webmanifest],
|
|
[.xml]
|
|
]
|
|
static let fileNames: [String] = [
|
|
"apple-touch-icon",
|
|
"error",
|
|
"favicon",
|
|
"icon",
|
|
"index",
|
|
"robots",
|
|
"shared",
|
|
"site",
|
|
"sitemap"
|
|
]
|
|
static let relativePaths: [[String]] = [
|
|
["apple-touch-icon.png"],
|
|
["css/error.css", "js/error.js"],
|
|
["favicon.ico"],
|
|
["icon.png", "icon.svg"],
|
|
["css/index.css", "js/index.js"],
|
|
["robots.txt"],
|
|
["css/shared.css", "js/shared.js"],
|
|
["site.webmanifest"],
|
|
["sitemap.xml"]
|
|
]
|
|
|
|
}
|