-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from polac24/20220204-overlay-mapper
Enable virtual file system overlay replacements
- Loading branch information
Showing
12 changed files
with
207 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
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
55 changes: 55 additions & 0 deletions
55
Sources/XCRemoteCache/Dependencies/OverlayDependenciesRemapper.swift
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,55 @@ | ||
// Copyright (c) 2021 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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. | ||
|
||
import Foundation | ||
|
||
/// File paths remapper according the virtual file system mappings | ||
/// Warning: this class is not thread safe | ||
class OverlayDependenciesRemapper: DependenciesRemapper { | ||
private var mappings: [OverlayMapping] | ||
|
||
init(overlayReader: OverlayReader) throws { | ||
mappings = try overlayReader.provideMappings() | ||
} | ||
|
||
private func mapPath( | ||
_ path: String, | ||
source: KeyPath<OverlayMapping,URL>, | ||
destination: KeyPath<OverlayMapping,URL> | ||
) -> String { | ||
guard let mapping = mappings.first(where: { $0[keyPath: source].path == path }) else { | ||
// TODO: support partial mappings, where a directory path can be replaced with some other directory | ||
// no direct mapping found | ||
return path | ||
} | ||
return mapping[keyPath: destination].path | ||
} | ||
|
||
func replace(genericPaths: [String]) -> [String] { | ||
Set(genericPaths.map { | ||
mapPath($0, source: \.virtual, destination: \.local) | ||
}).sorted() | ||
} | ||
|
||
func replace(localPaths: [String]) -> [String] { | ||
Set(localPaths.map { | ||
mapPath($0, source: \.local, destination: \.virtual) | ||
}).sorted() | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
Tests/XCRemoteCacheTests/Dependencies/OverlayDependenciesRemapperTests.swift
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,65 @@ | ||
// Copyright (c) 2021 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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. | ||
|
||
@testable import XCRemoteCache | ||
import XCTest | ||
|
||
class OverlayDependenciesRemapperTests: XCTestCase { | ||
private let overlayReader = OverlayReaderFake( | ||
mappings: [.init(virtual: "/file.h", local: "/Intermediate/Some/file.h")] | ||
) | ||
|
||
func testMappingFromLocalToGeneric() throws { | ||
let reader = try OverlayDependenciesRemapper( | ||
overlayReader: overlayReader | ||
) | ||
|
||
let dependencies = reader.replace(localPaths: ["/Intermediate/Some/file.h"]) | ||
XCTAssertEqual(dependencies, ["/file.h"]) | ||
} | ||
|
||
func testMappingFromGenericToLocal() throws { | ||
let reader = try OverlayDependenciesRemapper( | ||
overlayReader: overlayReader | ||
) | ||
|
||
let dependencies = reader.replace(genericPaths: ["/file.h"]) | ||
XCTAssertEqual(dependencies, ["/Intermediate/Some/file.h"]) | ||
} | ||
|
||
func testGenericDependenciesAreMerged() throws { | ||
|
||
let reader = try OverlayDependenciesRemapper( | ||
overlayReader: overlayReader | ||
) | ||
|
||
let dependencies = reader.replace(localPaths: ["/Intermediate/Some/file.h", "/file.h"]) | ||
XCTAssertEqual(dependencies, ["/file.h"]) | ||
} | ||
|
||
func testLocalDependenciesAreMerged() throws { | ||
let reader = try OverlayDependenciesRemapper( | ||
overlayReader: overlayReader | ||
) | ||
|
||
let dependencies = reader.replace(genericPaths: ["/Intermediate/Some/file.h", "/file.h"]) | ||
XCTAssertEqual(dependencies, ["/Intermediate/Some/file.h"]) | ||
} | ||
} | ||
|
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
1 change: 1 addition & 0 deletions
1
...s/XCRemoteCacheTests/TestData/Dependencies/JsonOverlayReaderTests/overlayReaderEmpty.json
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 @@ | ||
{"case-sensitive":"false","roots":[],"version":0} |
32 changes: 32 additions & 0 deletions
32
Tests/XCRemoteCacheTests/TestDoubles/OverlayReaderFake.swift
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,32 @@ | ||
// Copyright (c) 2021 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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. | ||
|
||
import Foundation | ||
@testable import XCRemoteCache | ||
|
||
class OverlayReaderFake: OverlayReader { | ||
private let mappings: [OverlayMapping] | ||
init(mappings: [OverlayMapping]) { | ||
self.mappings = mappings | ||
} | ||
|
||
func provideMappings() throws -> [OverlayMapping] { | ||
return mappings | ||
} | ||
} |
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