1
1
import XCTest
2
2
import CodeTimer
3
3
@testable import XcodeProj
4
+ import PBXProj
4
5
import SwiftPatches
5
6
6
7
#if os(Linux)
@@ -329,36 +330,319 @@ class XcodeProjTests: XCTestCase {
329
330
}
330
331
}
331
332
332
- func testAddFileToSwiftProject( ) {
333
- let workingDir : URL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) )
334
- let workingProjectName : String = " TestSwiftCommandLine "
335
- let projectFolder : URL = workingDir. appendingPathComponent ( workingProjectName, isDirectory: true )
336
- let sourcesFolder = projectFolder. appendingPathComponent ( " Sources " , isDirectory: true )
337
- let targetFolder = sourcesFolder. appendingPathComponent ( workingProjectName, isDirectory: true )
338
- defer {
339
- try ? FileManager . default. removeItem ( at: projectFolder)
340
- }
341
- do {
342
-
343
- let proj = try XcodeProjectBuilders . Swift. CommandLine. create ( workingProjectName,
344
- in: workingDir,
345
- havingUserDetails: . testUserDetails)
346
- let testFile : URL = URL ( fileURLWithPath: #file)
347
- let destTestFile : URL = targetFolder. appendingPathComponent ( testFile. lastPathComponent)
333
+ func testAddAndRemoveFilesFromSwiftProject( ) {
334
+ let workingDir : URL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) )
335
+ let workingProjectName : String = " TestSwiftCommandLine "
336
+ let groupName : String = " SubGroup "
337
+ let projectFolder : URL = workingDir. appendingPathComponent ( workingProjectName, isDirectory: true )
338
+ let sourcesFolder = projectFolder. appendingPathComponent ( " Sources " , isDirectory: true )
339
+ let targetFolder = sourcesFolder. appendingPathComponent ( workingProjectName, isDirectory: true )
340
+
341
+ defer {
342
+ try ? FileManager . default. removeItem ( at: projectFolder)
343
+ }
344
+ // Clear out any unclean project
345
+ try ? FileManager . default. removeItem ( at: projectFolder)
346
+
347
+ do {
348
+
349
+ var proj = try XcodeProjectBuilders . Swift. CommandLine. create ( workingProjectName,
350
+ in: workingDir,
351
+ havingUserDetails: . testUserDetails)
352
+
353
+ let projectPackagePath = proj. projectPackage
354
+ let testFile : URL = URL ( fileURLWithPath: #file)
355
+
356
+
357
+ var targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
358
+ var subGroup = try targetGroup. createGroup ( withName: groupName)
359
+
360
+ // Create copy file destination URL
361
+ let destTestFile : URL = subGroup. fullURL. url. appendingPathComponent ( testFile. lastPathComponent)
362
+
348
363
/// Copy test file into project
349
364
try FileManager . default. copyItem ( at: testFile, to: destTestFile)
350
-
351
- let targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
352
- let localResourceFile = targetGroup. fullURL. appendingFileComponent ( testFile. lastPathComponent)
353
-
354
- try targetGroup. addExisting ( localResourceFile,
355
- includeInTargets: [ proj. targets. first!] ,
356
- copyLocally: true ,
357
- savePBXFile: false )
358
- print ( proj)
359
- } catch {
360
- XCTFail ( " \( error) " )
361
- }
365
+
366
+ let localResourceFile = subGroup. fullURL. appendingFileComponent ( testFile. lastPathComponent)
367
+
368
+ var testSrcFile = try subGroup. addExisting ( localResourceFile,
369
+ includeInTargets: [ proj. targets. first!] ,
370
+ copyLocally: true ,
371
+ savePBXFile: true )
372
+
373
+ // Check to make sure build rule has been added
374
+ guard proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
375
+ XCTFail ( " Unable to find build file for source ' \( testSrcFile. name) ' " )
376
+ return
377
+ }
378
+
379
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Setup initial project") }
380
+
381
+ // Trying to remove reference to file
382
+ do {
383
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
384
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
385
+
386
+ guard let grp = targetGroup. group ( atPath: groupName) else {
387
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
388
+ return
389
+ }
390
+ subGroup = grp
391
+ guard let f = subGroup. file ( atPath: testFile. lastPathComponent) else {
392
+ XCTFail ( " Unable to locate file ' \( testFile. lastPathComponent) )' " )
393
+ return
394
+ }
395
+
396
+ try f. remove ( deletingFiles: false , savePBXFile: true )
397
+ // Verify file still exists on file system
398
+ guard try proj. fsProvider. itemExists ( at: f. fullURL) else {
399
+ XCTFail ( " File ' \( testFile. lastPathComponent) )' was deleted when expecting reference only be removed " )
400
+ return
401
+ }
402
+ // Checking to make sure the build file rule was removed
403
+ guard !proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
404
+ XCTFail ( " Found build file reference for ' \( testSrcFile. name) ' after file was removed " )
405
+ return
406
+ }
407
+
408
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Removed file reference") }
409
+ }
410
+
411
+ // Adding test file back to project
412
+ do {
413
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
414
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
415
+
416
+ guard let grp = targetGroup. group ( atPath: groupName) else {
417
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
418
+ return
419
+ }
420
+ subGroup = grp
421
+ guard subGroup. file ( atPath: testFile. lastPathComponent) == nil else {
422
+ XCTFail ( " Found file ' \( testFile. lastPathComponent) )' when expecting not to " )
423
+ return
424
+ }
425
+
426
+ let localResourceFile = subGroup. fullURL. appendingFileComponent ( testFile. lastPathComponent)
427
+
428
+ testSrcFile = try subGroup. addExisting ( localResourceFile,
429
+ includeInTargets: [ proj. targets. first!] ,
430
+ copyLocally: true ,
431
+ savePBXFile: true )
432
+
433
+ // Check to make sure build rule has been added
434
+ guard proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
435
+ XCTFail ( " Unable to find build file for source ' \( testSrcFile. name) ' " )
436
+ return
437
+ }
438
+
439
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Added file reference back") }
440
+ }
441
+ // Trying to remove folder reference and all child references
442
+ do {
443
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
444
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
445
+
446
+ guard let grp = targetGroup. group ( atPath: groupName) else {
447
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
448
+ return
449
+ }
450
+ subGroup = grp
451
+ guard let f = subGroup. file ( atPath: testFile. lastPathComponent) else {
452
+ XCTFail ( " Unable to locate file ' \( testFile. lastPathComponent) )' " )
453
+ return
454
+ }
455
+
456
+ try subGroup. remove ( deletingFiles: false , savePBXFile: true )
457
+
458
+ // Making sure group file reference has been removed
459
+ guard targetGroup. group ( atPath: groupName) == nil else {
460
+ XCTFail ( " Expected not to find group ' \( subGroup. name) )' after deleting " )
461
+ return
462
+ }
463
+ // Making sure file reference has been remvoed
464
+ guard targetGroup. resource ( atPath: " \( groupName) / \( testFile. lastPathComponent) " ) == nil else {
465
+ XCTFail ( " Expected not to find file ' \( testFile. lastPathComponent) )' after deleting " )
466
+ return
467
+ }
468
+
469
+ // Checking to make sure the build file rule was removed
470
+ guard !proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
471
+ XCTFail ( " Found build file reference for ' \( testSrcFile. name) ' after file was removed " )
472
+ return
473
+ }
474
+
475
+ // Verify folder still exists on file system
476
+ guard try proj. fsProvider. itemExists ( at: subGroup. fullURL) else {
477
+ XCTFail ( " Folder ' \( subGroup. name) )' was deleted when expecting reference only be removed " )
478
+ return
479
+ }
480
+ // Verify file still exists on file system
481
+ guard try proj. fsProvider. itemExists ( at: f. fullURL) else {
482
+ XCTFail ( " File ' \( testFile. lastPathComponent) )' was deleted when expecting reference only be removed " )
483
+ return
484
+ }
485
+
486
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Remvoed folder and child references back") }
487
+ }
488
+
489
+ // Adding test group and file back to project
490
+ do {
491
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
492
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
493
+ guard targetGroup. group ( atPath: groupName) == nil else {
494
+ XCTFail ( " Found group ' \( groupName) ' when expecting not to " )
495
+ return
496
+ }
497
+ let localResourceFile = targetGroup. fullURL. appendingDirComponent ( groupName)
498
+ try targetGroup. addExisting ( localResourceFile,
499
+ includeInTargets: [ proj. targets. first!] ,
500
+ copyLocally: true ,
501
+ savePBXFile: true )
502
+ guard let grp = targetGroup. group ( atPath: groupName) else {
503
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
504
+ return
505
+ }
506
+ subGroup = grp
507
+
508
+ guard let f = subGroup. file ( atPath: testFile. lastPathComponent) else {
509
+ XCTFail ( " Unable to locate file ' \( testFile. lastPathComponent) )' " )
510
+ return
511
+ }
512
+ testSrcFile = f
513
+
514
+ // Checking to make sure the build file rule was removed
515
+ guard proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
516
+ XCTFail ( " Unable to find build file for source ' \( testSrcFile. name) ' " )
517
+ return
518
+ }
519
+
520
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Added folder and child references back") }
521
+
522
+
523
+ }
524
+
525
+ // Deleting test file reference and physical file
526
+ do {
527
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
528
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
529
+
530
+ guard let grp = targetGroup. group ( atPath: groupName) else {
531
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
532
+ return
533
+ }
534
+ subGroup = grp
535
+ guard let f = subGroup. file ( atPath: testFile. lastPathComponent) else {
536
+ XCTFail ( " Unable to locate file ' \( testFile. lastPathComponent) )' " )
537
+ return
538
+ }
539
+ testSrcFile = f
540
+
541
+ try f. remove ( deletingFiles: true , savePBXFile: true )
542
+
543
+ // Checking to make sure the build file rule was removed
544
+ guard !proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
545
+ XCTFail ( " Found build file reference for ' \( testSrcFile. name) ' after file was removed " )
546
+ return
547
+ }
548
+
549
+
550
+ // Verify file is gone
551
+ guard !( try proj. fsProvider. itemExists ( at: f. fullURL) ) else {
552
+ XCTFail ( " File ' \( testFile. lastPathComponent) )' was NOT deleted when expecting reference and file removed " )
553
+ return
554
+ }
555
+
556
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Deleted file reference and file") }
557
+
558
+ }
559
+
560
+ // Copy test file back into project
561
+ do {
562
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
563
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
564
+
565
+ guard let grp = targetGroup. group ( atPath: groupName) else {
566
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
567
+ return
568
+ }
569
+ subGroup = grp
570
+
571
+ // Making sure file reference has been removed
572
+ guard subGroup. file ( atPath: testFile. lastPathComponent) == nil else {
573
+ XCTFail ( " Expected not to find file ' \( testFile. lastPathComponent) )' after deleting " )
574
+ return
575
+ }
576
+
577
+ // Create copy file destination URL
578
+ let destTestFile : URL = subGroup. fullURL. url. appendingPathComponent ( testFile. lastPathComponent)
579
+
580
+ /// Copy test file into project
581
+ try FileManager . default. copyItem ( at: testFile, to: destTestFile)
582
+
583
+ let localResourceFile = subGroup. fullURL. appendingFileComponent ( testFile. lastPathComponent)
584
+
585
+ let testSrcFile = try subGroup. addExisting ( localResourceFile,
586
+ includeInTargets: [ proj. targets. first!] ,
587
+ copyLocally: true ,
588
+ savePBXFile: true )
589
+
590
+ // Check to make sure build rule has been added
591
+ guard proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
592
+ XCTFail ( " Unable to find build file for source ' \( testSrcFile. name) ' " )
593
+ return
594
+ }
595
+
596
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Re-added test file") }
597
+
598
+ }
599
+
600
+ // Delete subgroup and child files
601
+ do {
602
+ proj = try XcodeProject ( fromURL: projectPackagePath. url)
603
+ targetGroup = proj. resources. group ( atPath: " Sources/ \( workingProjectName) " ) !
604
+
605
+ guard let grp = targetGroup. group ( atPath: groupName) else {
606
+ XCTFail ( " Unable to locate subgroup ' \( groupName) ' " )
607
+ return
608
+ }
609
+ subGroup = grp
610
+
611
+ guard let f = subGroup. file ( atPath: testFile. lastPathComponent) else {
612
+ XCTFail ( " Unable to locate file ' \( testFile. lastPathComponent) )' " )
613
+ return
614
+ }
615
+ testSrcFile = f
616
+
617
+ try subGroup. remove ( deletingFiles: true , savePBXFile: true )
618
+
619
+ // Checking to make sure the build file rule was removed
620
+ guard !proj. targets. first!. sourcesBuildPhase ( ) . files. contains ( where: { $0. fileRef == testSrcFile. pbxFileResource. id } ) else {
621
+ XCTFail ( " Found build file reference for ' \( testSrcFile. name) ' after file was removed " )
622
+ return
623
+ }
624
+
625
+
626
+ // Verify file is gone
627
+ guard !( try proj. fsProvider. itemExists ( at: f. fullURL) ) else {
628
+ print ( " File: " + f. fullURL. url. path)
629
+ XCTFail ( " File ' \( testFile. lastPathComponent) )' was NOT deleted when expecting reference and file removed " )
630
+ return
631
+ }
632
+
633
+ // Verify folder is gone
634
+ guard !( try proj. fsProvider. itemExists ( at: subGroup. fullURL) ) else {
635
+ print ( " Folder: " + subGroup. fullURL. url. path)
636
+ XCTFail ( " Folder ' \( testFile. lastPathComponent) )' was NOT deleted when expecting reference and folder removed " )
637
+ return
638
+ }
639
+
640
+ //if isXcodeTesting { print("testAddAndRemoveFilesFromSwiftProject: Deleted sub group and child files") }
641
+ }
642
+
643
+ } catch {
644
+ XCTFail ( " \( error) " )
645
+ }
362
646
}
363
647
364
648
func testEmpty( ) { }
@@ -371,6 +655,7 @@ class XcodeProjTests: XCTestCase {
371
655
( " testOtherProjects " , testOtherProjects) ,
372
656
( " testCreateSwiftCommandLineApp " , testCreateSwiftCommandLineApp) ,
373
657
( " testCreateObjCCommandLineApp " , testCreateObjCCommandLineApp) ,
374
- ( " testCreateOtherEmptyProject " , testCreateOtherEmptyProject)
658
+ ( " testCreateOtherEmptyProject " , testCreateOtherEmptyProject) ,
659
+ ( " testAddAndRemoveFilesFromSwiftProject " , testAddAndRemoveFilesFromSwiftProject)
375
660
]
376
661
}
0 commit comments