<commit_before>import XCTest
@testable import TUSKit

final class FilesTests: XCTestCase {

    var files: Files!
    override func setUp() {
        super.setUp()
        
        files = Files(storageDirectory: URL(string: "TUS")!)
        do {
            try files.clearCacheInStorageDirectory()
        } catch {
            XCTFail("Could not clear dir \(error)")
        }
    }
    
    override func tearDown() {
        do {
            try files.clearCacheInStorageDirectory()
            try emptyCacheDir()
        } catch {
//            XCTFail("Could not clear cache \(error)")
        }
    }
    
    private func emptyCacheDir() throws {
        
        let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
        guard FileManager.default.fileExists(atPath: cacheDirectory.path, isDirectory: nil) else {
            return
        }
        
        for file in try FileManager.default.contentsOfDirectory(atPath: cacheDirectory.path) {
            try FileManager.default.removeItem(atPath: cacheDirectory.appendingPathComponent(file).path)
        }

    }
    
    func testInitializers() {
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let cacheDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        
        let values = [
            (URL(string: "ABC")!, documentsDirectory.appendingPathComponent("ABC")),
            (URL(string: "/ABC")!, documentsDirectory.appendingPathComponent("ABC")),
            (URL(string: "ABC/ZXC")!, documentsDirectory.appendingPathComponent("ABC/ZXC")),
            (URL(string: "/ABC/ZXC")!, documentsDirectory.appendingPathComponent("ABC/ZXC")),
            (nil, documentsDirectory.appendingPathComponent("TUS")),
            (cacheDirectory.appendingPathComponent("TEST"), cacheDirectory.appendingPathComponent("TEST"))
            ]
        
        for (url, expectedPath) in values {
            let files = Files(storageDirectory: url)
            XCTAssertEqual(expectedPath, files.storageDirectory)
        }
    }
    
    func testCopyingFileFromURL() throws {
        let path = try Fixtures.makeFilePath()
        let id = UUID()
        let url = try files.copy(from: path, id: id)
        
        XCTAssert(url.lastPathComponent.contains(id.uuidString), "Expected path to contain id")
        
        let _ = try Data(contentsOf: url)
    }
    
    func testStoringData() throws {
        let id = UUID()
        let url = try files.store(data: Fixtures.loadData(), id: id)
        XCTAssert(url.lastPathComponent.contains(id.uuidString), "Expected path to contain id")
        let _ = try Data(contentsOf: url)
    }
    <commit_msg>Making FilesTest less picky about trailing slashes.<commit_after>import XCTest
@testable import TUSKit

final class FilesTests: XCTestCase {

    var files: Files!
    override func setUp() {
        super.setUp()
        
        files = Files(storageDirectory: URL(string: "TUS")!)
        do {
            try files.clearCacheInStorageDirectory()
        } catch {
            XCTFail("Could not clear dir \(error)")
        }
    }
    
    override func tearDown() {
        do {
            try files.clearCacheInStorageDirectory()
            try emptyCacheDir()
        } catch {
//            XCTFail("Could not clear cache \(error)")
        }
    }
    
    private func emptyCacheDir() throws {
        
        let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
        guard FileManager.default.fileExists(atPath: cacheDirectory.path, isDirectory: nil) else {
            return
        }
        
        for file in try FileManager.default.contentsOfDirectory(atPath: cacheDirectory.path) {
            try FileManager.default.removeItem(atPath: cacheDirectory.appendingPathComponent(file).path)
        }

    }
    
    func testInitializers() {
        func removeTrailingSlash(url: URL) -> String {
            if url.absoluteString.last == "/" {
                return String(url.absoluteString.dropLast())
            } else {
                return url.absoluteString
            }
        }
            
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let cacheDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        
        let values = [
            (URL(string: "ABC")!, documentsDirectory.appendingPathComponent("ABC")),
            (URL(string: "/ABC")!, documentsDirectory.appendingPathComponent("ABC")),
            (URL(string: "ABC/ZXC")!, documentsDirectory.appendingPathComponent("ABC/ZXC")),
            (URL(string: "/ABC/ZXC")!, documentsDirectory.appendingPathComponent("ABC/ZXC")),
            (nil, documentsDirectory.appendingPathComponent("TUS")),
            (cacheDirectory.appendingPathComponent("TEST"), cacheDirectory.appendingPathComponent("TEST"))
            ]
        
        for (url, expectedPath) in values {
            let files = Files(storageDirectory: url)
            
            // Depending on the OS, there might be trailing slashes at the end of the path, that's okay.
            let trimmedExpectedPath = removeTrailingSlash(url: expectedPath)
            let trimmedPath = removeTrailingSlash(url: files.storageDirectory)
            
            XCTAssertEqual(trimmedPath, trimmedExpectedPath)
        }
    }
    
    func testCopyingFileFromURL() throws {
        let path = try Fixtures.makeFilePath()
        let id = UUID()
        let url = try files.copy(from: path, id: id)
        
        XCTAssert(url.lastPathComponent.contains(id.uuidString), "Expected path to contain id")
        
        let _ = try Data(contentsOf: url)
    }
    
    func testStoringData() throws {
        let id = UUID()
        let url = try files.store(data: Fixtures.loadData(), id: id)
        XCTAssert(url.lastPathComponent.contains(id.uuidString), "Expected path to contain id")
        let _ = try Data(contentsOf: url)
    }
    
{'fraction_non_alphanumeric': 0.10447028000654986, 'fraction_numerical': 0.0009824791223186507, 'mean_word_length': 2.296276308688613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5088702', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1649, 'n_words': 366}