Allow testonly validations on non-testonly targets

It should be OK to have a testonly validation validate a non-testonly
target. For example, a validation may itself need to depend on testonly
targets, and this does not affect the non-testonly target getting
validated: it still has no testonly code included in it.

Bug: 484745924
Change-Id: Ibe678cab77f01f72b77f0a8f1e0844f9e53b2b26
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/24000
Reviewed-by: Tomasz Śniatowski <tomasz.sniatowski@xperi.com>
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Reviewed-by: Andrew Grieve <agrieve@google.com>
diff --git a/src/gn/target.cc b/src/gn/target.cc
index c6cef20..a11a014 100644
--- a/src/gn/target.cc
+++ b/src/gn/target.cc
@@ -1222,14 +1222,6 @@
     }
   }
 
-  // Verify no validations have "testonly" set.
-  for (const auto& pair : validations_) {
-    if (pair.ptr->testonly()) {
-      *err = MakeTestOnlyError(this, pair.ptr);
-      return false;
-    }
-  }
-
   // Verify no configs have "testonly" set.
   for (ConfigValuesIterator iter(this); !iter.done(); iter.Next()) {
     if (const Config* config = iter.GetCurrentConfig()) {
diff --git a/src/gn/target_unittest.cc b/src/gn/target_unittest.cc
index 81c93ef..7ed4055 100644
--- a/src/gn/target_unittest.cc
+++ b/src/gn/target_unittest.cc
@@ -372,6 +372,12 @@
   product.set_testonly(false);
   product.private_deps().push_back(LabelTargetPair(&testlib));
   ASSERT_FALSE(product.OnResolved(&err));
+
+  // "package" is a non-test with a testonly validation, this is OK.
+  TestTarget package(setup, "//app:package", Target::EXECUTABLE);
+  package.set_testonly(false);
+  package.validations().push_back(LabelTargetPair(&test));
+  ASSERT_TRUE(package.OnResolved(&err));
 }
 
 // Configs can be testonly too.