Sort test file references used for indexing XCUITest test cases

By sorting the list of files we ensure that the generated file
is always the same (previously the order of the file depended
on the order where the BUILD.gn files were parsed which is not
deterministic).

Ensuring the generated project is deterministic makes it easier
to compare projects between different runs (which can be useful
when trying to change the content of the generated project to
bring it in sync which what new version of Xcode expects).

Bug: chromium/1071055
Change-Id: I62bbfd5c4112c031cb314694a69e259b5119eb0a
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/8080
Reviewed-by: Brett Wilson <brettw@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
diff --git a/src/gn/xcode_writer.cc b/src/gn/xcode_writer.cc
index 7e9b7db..4c3e285 100644
--- a/src/gn/xcode_writer.cc
+++ b/src/gn/xcode_writer.cc
@@ -251,12 +251,12 @@
 
 // Add xctest files to the "Compiler Sources" of corresponding test module
 // native targets.
-void AddXCTestFilesToTestModuleTarget(const SourceFileSet& xctest_file_list,
+void AddXCTestFilesToTestModuleTarget(const std::vector<SourceFile>& sources,
                                       PBXNativeTarget* native_target,
                                       PBXProject* project,
                                       SourceDir source_dir,
                                       const BuildSettings* build_settings) {
-  for (const SourceFile& source : xctest_file_list) {
+  for (const SourceFile& source : sources) {
     std::string source_path = RebasePath(source.value(), source_dir,
                                          build_settings->root_path_utf8());
 
@@ -631,13 +631,18 @@
       target_with_xctest_files = target;
     }
 
-    const SourceFileSet& xctest_file_list =
+    const SourceFileSet& sources =
         resolver.SearchFilesForTarget(target_with_xctest_files);
 
+    // Sort files to ensure deterministic generation of the project file (and
+    // nicely sorted file list in Xcode).
+    std::vector<SourceFile> sorted_sources(sources.begin(), sources.end());
+    std::sort(sorted_sources.begin(), sorted_sources.end());
+
     // Add xctest files to the "Compiler Sources" of corresponding xctest
     // and xcuitest native targets for proper indexing and for discovery of
     // tests function.
-    AddXCTestFilesToTestModuleTarget(xctest_file_list, pair.second, &project_,
+    AddXCTestFilesToTestModuleTarget(sorted_sources, pair.second, &project_,
                                      source_dir, build_settings_);
   }