Consider Xcode console as not a tty

When running gn_unittest under Xcode, isatty(1) returns true but
the ANSI escape sequences are not supported, resulting in weird
output.

Try to detect whether the output is send to Xcode's console by
checking whether some of the environment variables set by Xcode
are present.

Bug: none
Change-Id: I389f59495611c4e0e8537f7f13434211f8c89e0c
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/7060
Reviewed-by: Brett Wilson <brettw@chromium.org>
Commit-Queue: Brett Wilson <brettw@chromium.org>
diff --git a/src/util/test/gn_test.cc b/src/util/test/gn_test.cc
index 32e3c01..6e9b964 100644
--- a/src/util/test/gn_test.cc
+++ b/src/util/test/gn_test.cc
@@ -146,7 +146,14 @@
 #if defined(OS_WIN)
   if (enable_vt_processing.is_valid())
 #else
-  if (isatty(1))
+  // When run from Xcode, the console returns "true" to isatty(1) but it
+  // does not interprets ANSI escape sequence resulting in difficult to
+  // read output. There is no portable way to detect if the console is
+  // Xcode's console (term is set to xterm or xterm-256colors) but Xcode
+  // sets the __XCODE_BUILT_PRODUCTS_DIR_PATHS environment variable. Use
+  // this as a proxy to detect that the console does not interpret the
+  // ANSI sequences correctly.
+  if (isatty(1) && getenv("__XCODE_BUILT_PRODUCTS_DIR_PATHS") == NULL)
 #endif
   {
     prefix = "\r";