<commit_before>import Foundation
import PathKit
import SwiftShell
import XCTest
@testable import xcodeproj

class PBXBatchUpdaterTests: XCTestCase {
    func test_addFile_useMainProjectGroup() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let filePath = "\(sourceRoot.string)\(mainGroupPath)/\(fileName)"
            Files.createFile(atPath: filePath, contents: nil, attributes: nil)
            let file = try updater.addFile(to: project, at: Path(filePath))
            let fileFullPath = try file.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                fileFullPath,
                Path(filePath)
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, 1)
    }

    func test_addFile_withSubgroups() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let subgroupNames = (0 ... 5).map { _ in UUID().uuidString }
        let expectedGroupCount = 1 + subgroupNames.count
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let subgroupPath = subgroupNames.joined(separator: "/")
            let filePath = "\(sourceRoot.string)\(mainGroupPath)/\(subgroupPath)/\(fileName)"
            Files.createFile(atPath: filePath, contents: nil, attributes: nil)
            let file = try updater.addFile(to: project, at: Path(filePath))
            let fileFullPath = try file.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                fileFullPath,
                Path(filePath)
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, expectedGroupCount)
    }

    func test_addFile_byFileName() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let mainGroup = project.mainGroup!
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let filePath = "\(sourceRoot.string)\(mainGroupPath)/\(fileName)"
            Files.createFile(atPath: filePath, contents: nil, attributes: nil)
            let file = try updater.addFile(to: mainGroup, fileName: fileName)
            let fileFullPath = try file.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                fileFullPath,
                Path(filePath)
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, 1)
    }

    func test_addFile_alreadyExisted() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let mainGroup = project.mainGroup!
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let filePath = "\(sourceRoot.string)\(mainGroupPath)/\(fileName)"
            Files.createFile(atPath: filePath, contents: nil, attributes: nil)
            let firstFile = try updater.addFile(to: project, at: Path(filePath))
            let secondFile = try updater.addFile(to: mainGroup, fileName: fileName)
            let firstFileFullPath = try firstFile.fullPath(sourceRoot: sourceRoot)
            let secondFileFullPath = try secondFile.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                firstFileFullPath,
                Path(filePath)
            )
            XCTAssertEqual(
                secondFileFullPath,
                Path(filePath)
            )
            XCTAssertEqual(
                firstFile,
                secondFile
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, 1)
    }

    func test_addFile_alreadyExistedWithSubgroups() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let subgroupNames = (0 ... 5).map { _ in UUID().uuidString }
        let expectedGroupCount = 1 + subgroupNames.count
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let subgroupPath = subgroupNames.joined(separator: "/")
            let filePath = "\(sourceRoot.string)\(mainGroupPath)/\(subgroupPath)/\(fileName)"
            Files.createFile(atPath: filePath, contents: nil, attributes: nil)
            let firstFile = try updater.addFile(to: project, at: Path(filePath))
            let parentGroup = proj.groups.first(where: { $0.path == subgroupNames.last! })!
            let secondFile = try updater.addFile(to: parentGroup, fileName: fileName)
            let firstFileFullPath = try firstFile.fullPath(sourceRoot: sourceRoot)
            let secondFileFullPath = try secondFile.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                firstFileFullPath,
                Path(filePath)
            )
            XCTAssertEqual(
                secondFileFullPath,
                Path(filePath)
            )
            XCTAssertEqual(
                firstFile,<commit_msg>Update PBXBatchUpdaterTests<commit_after>import Foundation
import PathKit
import XCTest
@testable import xcodeproj

class PBXBatchUpdaterTests: XCTestCase {
    func test_addFile_useMainProjectGroup() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let filePath = Path("\(sourceRoot.string)\(mainGroupPath)/\(fileName)")
            try Data().write(to: filePath.url)
            let file = try updater.addFile(to: project, at: filePath)
            let fileFullPath = try file.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                fileFullPath,
                filePath
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, 1)
    }

    func test_addFile_withSubgroups() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let subgroupNames = (0 ... 5).map { _ in UUID().uuidString }
        let expectedGroupCount = 1 + subgroupNames.count
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let subgroupPath = subgroupNames.joined(separator: "/")
            let filePath = Path("\(sourceRoot.string)\(mainGroupPath)/\(subgroupPath)/\(fileName)")
            try Data().write(to: filePath.url)
            let file = try updater.addFile(to: project, at: filePath)
            let fileFullPath = try file.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                fileFullPath,
                filePath
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, expectedGroupCount)
    }

    func test_addFile_byFileName() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let mainGroup = project.mainGroup!
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let filePath = Path("\(sourceRoot.string)\(mainGroupPath)/\(fileName)")
            try Data().write(to: filePath.url)
            let file = try updater.addFile(to: mainGroup, fileName: fileName)
            let fileFullPath = try file.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                fileFullPath,
                filePath
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, 1)
    }

    func test_addFile_alreadyExisted() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let mainGroup = project.mainGroup!
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let filePath = Path("\(sourceRoot.string)\(mainGroupPath)/\(fileName)")
            try Data().write(to: filePath.url)
            let firstFile = try updater.addFile(to: project, at: filePath)
            let secondFile = try updater.addFile(to: mainGroup, fileName: fileName)
            let firstFileFullPath = try firstFile.fullPath(sourceRoot: sourceRoot)
            let secondFileFullPath = try secondFile.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                firstFileFullPath,
                filePath
            )
            XCTAssertEqual(
                secondFileFullPath,
                filePath
            )
            XCTAssertEqual(
                firstFile,
                secondFile
            )
        }

        XCTAssertEqual(proj.fileReferences.count, 2)
        XCTAssertEqual(proj.groups.count, 1)
    }

    func test_addFile_alreadyExistedWithSubgroups() {
        let sourceRoot = Path.temporary
        let mainGroupPath = UUID().uuidString
        let proj = fillAndCreateProj(
            sourceRoot: sourceRoot,
            mainGroupPath: mainGroupPath
        )
        let project = proj.projects.first!
        let subgroupNames = (0 ... 5).map { _ in UUID().uuidString }
        let expectedGroupCount = 1 + subgroupNames.count
        try! proj.batchUpdate(sourceRoot: sourceRoot) { updater in
            let fileName = "file.swift"
            let subgroupPath = subgroupNames.joined(separator: "/")
            let filePath = Path("\(sourceRoot.string)\(mainGroupPath)/\(subgroupPath)/\(fileName)")
            try Data().write(to: filePath.url)
            let firstFile = try updater.addFile(to: project, at: filePath)
            let parentGroup = proj.groups.first(where: { $0.path == subgroupNames.last! })!
            let secondFile = try updater.addFile(to: parentGroup, fileName: fileName)
            let firstFileFullPath = try firstFile.fullPath(sourceRoot: sourceRoot)
            let secondFileFullPath = try secondFile.fullPath(sourceRoot: sourceRoot)
            XCTAssertEqual(
                firstFileFullPath,
                filePath
            )
            XCTAssertEqual(
                secondFileFullPath,
                filePath
            )
            XCTAssertEqual(
                firstFile,
{'fraction_non_alphanumeric': 0.08517959046659954, 'fraction_numerical': 0.0023497818059751594, 'mean_word_length': 2.0230847285641804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6811210', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3204, 'n_words': 687}