Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ jobs:
name: Linux SPM unit tests
runs-on: ubuntu-latest
container:
image: swift:5.10
image: swift:6.0
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Run tests
run: swift test

Expand All @@ -25,8 +27,10 @@ jobs:
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '16.0-beta'
xcode-version: '16.0'
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: SPM tests
run: make test
- name: Normalize coverage info
Expand All @@ -43,8 +47,10 @@ jobs:
steps:
- uses: compnerd/gha-setup-swift@main
with:
branch: swift-5.10-release
tag: 5.10-RELEASE
branch: swift-6.0-release
tag: 6.0-RELEASE
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Run tests
run: swift test -v --enable-test-discovery
2 changes: 1 addition & 1 deletion .jazzy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ theme: fw2020
deployment_url: https://johnfairh.github.io/SourceMapper/
build_tool: spm
modules: SourceMapper
module_version: v2.0.0
module_version: v3.0.0
custom_groups:
- name: Types
children:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ test:
swift test --enable-code-coverage

test_linux:
docker run -v `pwd`:`pwd` -w `pwd` --name SourceMapper --rm swift:5.7 swift test
docker run -v `pwd`:`pwd` -w `pwd` --name SourceMapper --rm swift:6.0 swift test

shell_linux:
docker run -it -v `pwd`:`pwd` -w `pwd` --name SourceMapper --rm swift:5.7 /bin/bash
docker run -it -v `pwd`:`pwd` -w `pwd` --name SourceMapper --rm swift:6.0 /bin/bash

install: build
-mkdir -p ${PREFIX}/bin
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:6.0

import PackageDescription

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ No support for:

## Requirements

* Swift 5.7
* macOS 10.10 (tested on macOS 12.0 IA64)
* Swift 6.0
* macOS 14 (tested on macOS 14.6.1 IA64)
* Linux (tested on Ubuntu 18.04.5)
* Windows 10, Swift 5.7 (tested in CI only)
* Windows 10, Swift 6.0 (tested in CI only)

## Installation

Expand All @@ -56,7 +56,7 @@ Package dependency:
```swift
.package(name: "SourceMapper",
url: "https://github.com/johnfairh/SourceMapper.git",
from: "2.0.0")
from: "3.0.0")
```

## Contributions
Expand Down
4 changes: 4 additions & 0 deletions Sources/Cli/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//

import SourceMapper

#if canImport(Glibc)
@preconcurrency import Glibc
#endif
import Foundation

let args = ProcessInfo.processInfo.arguments
Expand Down
83 changes: 45 additions & 38 deletions Tests/SourceMapperTests/TestBasics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,63 @@
//

@testable import SourceMapper
import XCTest
import Testing
import Foundation

extension Tag {
@Tag static var basics: Self
}

/// Basic flows of the use cases
class TestBasics: XCTestCase {
@Suite(.tags(.basics))
final class TestBasics {
@Test
func testEmptyRoundTrip() throws {
let empty = SourceMap()
let serialized = try empty.encode()
let deserialized = try SourceMap(serialized)
XCTAssertEqual(empty, deserialized)
try XCTAssertEqual(empty.encodeString(), deserialized.encodeString())
#expect(empty == deserialized)
#expect(try empty.encodeString() == deserialized.encodeString())
}

func testLoading() throws {
let files = ["jazzy.css.map.dart", "jazzy.css.map.libsass"]
try files.forEach { fixtureName in
let map = try SourceMap(fixtureName: fixtureName)
XCTAssertEqual(SourceMap.VERSION, map.version)
let file = try XCTUnwrap(map.file)
XCTAssertEqual(fixtureName.replacingOccurrences(of: ".map", with: ""), file)
XCTAssertEqual(1, map.sources.count)
XCTAssertTrue(map.sources[0].url.hasSuffix("jazzy.css.scss"))
@Test(arguments: ["jazzy.css.map.dart", "jazzy.css.map.libsass"])
func testLoading(fixtureName: String) throws {
let map = try SourceMap(fixtureName: fixtureName)
#expect(map.version == SourceMap.VERSION)
let file = try #require(map.file)
#expect(file == fixtureName.replacingOccurrences(of: ".map", with: ""))
#expect(map.sources.count == 1)
#expect(map.sources[0].url.hasSuffix("jazzy.css.scss"))

print(map)
let unpackedMap = try UnpackedSourceMap(map)
print(unpackedMap.segmentsDescription)
print(map)
let unpackedMap = try UnpackedSourceMap(map)
print(unpackedMap.segmentsDescription)

// Check a couple of mapping positions, one towards the start and
// one at the end to check the mapping accumulators.
// Check a couple of mapping positions, one towards the start and
// one at the end to check the mapping accumulators.

let mapped1 = try XCTUnwrap(unpackedMap.map(line: 26, column: 22))
let pos1 = try XCTUnwrap(mapped1.sourcePos)
XCTAssertEqual(25, pos1.line)
XCTAssertTrue(pos1.column >= 14)
let mapped1 = try #require(unpackedMap.map(line: 26, column: 22))
let pos1 = try #require(mapped1.sourcePos)
#expect(pos1.line == 25)
#expect(pos1.column >= 14)

let mapped2 = try XCTUnwrap(unpackedMap.map(line: 465, column: 12))
let pos2 = try XCTUnwrap(mapped2.sourcePos)
XCTAssertEqual(601, pos2.line)
XCTAssertTrue(pos2.column >= 4)
}
let mapped2 = try #require(unpackedMap.map(line: 465, column: 12))
let pos2 = try #require(mapped2.sourcePos)
#expect(pos2.line == 601)
#expect(pos2.column >= 4)
}

@Test
func testPrinting() throws {
var map = SourceMap()
XCTAssertTrue(try UnpackedSourceMap(map).segmentsDescription.isEmpty)
XCTAssertEqual(#"SourceMap(v=3 #sources=0 mappings="")"#, map.description)
try #expect(UnpackedSourceMap(map).segmentsDescription.isEmpty)
#expect(map.description == #"SourceMap(v=3 #sources=0 mappings="")"#)

map.file = "myfile.css"
map.sourceRoot = "../dist"
map.sources = [.init(url: "a.scss")]
map.names = ["fred", "barney"]
XCTAssertEqual(#"SourceMap(v=3 file="myfile.css" sourceRoot="../dist" #sources=1 #names=2 mappings="")"#, map.description)
#expect(map.description == #"SourceMap(v=3 file="myfile.css" sourceRoot="../dist" #sources=1 #names=2 mappings="")"#)

try map.set(segments: [
[
Expand All @@ -65,15 +71,16 @@ class TestBasics: XCTestCase {
]
])
let segDesc = try UnpackedSourceMap(map).segmentsDescription
XCTAssertEqual("""
line=0 col=0-12 (source=0 line=0 col=0 name=1)
col=13 (source=0 line=1 col=0 name=1)
""", segDesc)
#expect(segDesc == """
line=0 col=0-12 (source=0 line=0 col=0 name=1)
col=13 (source=0 line=1 col=0 name=1)
""")
_ = try map.encode() // to encode the mapping string
print(map.description)
XCTAssertTrue(map.description.hasSuffix(#" mappings="AAAAC,aACAA")"#))
#expect(map.description.hasSuffix(#" mappings="AAAAC,aACAA")"#))
}

@Test
func testSourceURL() throws {
var map = SourceMap()
map.sources = [.init(url: "http://host/path/a.scss"),
Expand All @@ -82,13 +89,13 @@ class TestBasics: XCTestCase {
let mapURL = URL(fileURLWithPath: "/web/main.map")

let source1 = map.getSourceURL(source: 0, sourceMapURL: mapURL)
XCTAssertEqual("http://host/path/a.scss", source1.absoluteString)
#expect(source1.absoluteString == "http://host/path/a.scss")

let source2 = map.getSourceURL(source: 1, sourceMapURL: mapURL)
XCTAssertEqual("file:///dist/b.scss", source2.absoluteString)
#expect(source2.absoluteString == "file:///dist/b.scss")

map.sourceRoot = "./../dist/"
let source3 = map.getSourceURL(source: 2, sourceMapURL: mapURL)
XCTAssertEqual("file:///dist/c.scss", source3.absoluteString)
#expect(source3.absoluteString == "file:///dist/c.scss")
}
}
23 changes: 0 additions & 23 deletions Tests/SourceMapperTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
import SourceMapper
import XCTest

extension SourceMap {
init(url: URL) throws {
Expand All @@ -22,25 +21,3 @@ extension SourceMap {
try self.init(url: Self.fixturesURL.appendingPathComponent(fixtureName))
}
}

func XCTAssertThrows<T: Error>(_ errType: T.Type, _ callback: () throws -> Void) {
do {
try callback()
} catch let error as T {
print(error)
} catch {
XCTFail("Unexpected error: \(error)")
}
}

func XCTAssertSourceMapError(_ err: SourceMapError, _ callback: () throws -> Void) {
do {
try callback()
XCTFail("Did not throw any errors")
} catch let error as SourceMapError {
XCTAssertEqual(err, error)
print(error)
} catch {
XCTFail("Unexpected error: \(error)")
}
}
36 changes: 23 additions & 13 deletions Tests/SourceMapperTests/TestJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
//

@testable import SourceMapper
import XCTest
import Testing

extension Tag {
@Tag static var json: Self
}

/// JSON code/decode logic and error handling
class TestJSON: XCTestCase {
@Suite(.tags(.json))
final class TestJSON {
@Test
func testMissingFields() throws {
XCTAssertThrows(Swift.DecodingError.self) {
#expect(throws: Swift.DecodingError.self) {
let map = try SourceMap("{}")
XCTFail("Managed to decode bad map: \(map)")
print("Bad map decoded: \(map)")
}
}

@Test
func testBadVersion() throws {
let badVersionJSON = """
{
Expand All @@ -27,12 +34,13 @@ class TestJSON: XCTestCase {
}
"""

XCTAssertSourceMapError(.invalidFormat(4)) {
#expect(throws: SourceMapError.invalidFormat(4)) {
let map = try SourceMap(badVersionJSON)
XCTFail("Managed to decode bad map: \(map)")
print("Bad map decoded: \(map)")
}
}

@Test
func testInconsistentSources() throws {
let badSourcesJSON = """
{
Expand All @@ -44,12 +52,13 @@ class TestJSON: XCTestCase {
}
"""

XCTAssertSourceMapError(.inconsistentSources(sourcesCount: 3, sourcesContentCount: 2)) {
#expect(throws: SourceMapError.inconsistentSources(sourcesCount: 3, sourcesContentCount: 2)) {
let map = try SourceMap(badSourcesJSON)
XCTFail("Managed to decode bad map: \(map)")
print("Bad map decoded: \(map)")
}
}

@Test
func testSourceContent() throws {
let sourcedJSON = """
{
Expand All @@ -61,15 +70,16 @@ class TestJSON: XCTestCase {
}
"""
let map = try SourceMap(sourcedJSON)
XCTAssertEqual(2, map.sources.count)
XCTAssertEqual("contents of a", map.sources[0].content)
XCTAssertNil(map.sources[1].content)
#expect(map.sources.count == 2)
#expect("contents of a" == map.sources[0].content)
#expect(map.sources[1].content == nil)

let encoded = try map.encode()
let map2 = try SourceMap(encoded)
XCTAssertEqual(map, map2)
#expect(map == map2)
}

@Test
func testBadMapping() throws {
let json = """
{
Expand All @@ -79,7 +89,7 @@ class TestJSON: XCTestCase {
"mappings": "AAA"
}
"""
XCTAssertSourceMapError(.invalidVLQStringLength([0,0,0])) {
#expect(throws: SourceMapError.invalidVLQStringLength([0,0,0])) {
_ = try SourceMap(json).segments
}
}
Expand Down
Loading