Introduce artifact_path() for rust

This makes it so that rust tests inside of AOS do not have to care about
whether the repository is named "aos", "org_frc971", etc. This also
takes the opportunity to fix up the C++ equivalent to not even have the
repo name hard-coded in the library.

Change-Id: Ie3114c6a420fa46c1cc0f7acfe8115232b10eae3
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/BUILD b/aos/BUILD
index e67b593..880985f 100644
--- a/aos/BUILD
+++ b/aos/BUILD
@@ -346,6 +346,9 @@
     test_data = [
         "//aos/testdata:test_configs",
     ],
+    test_deps = [
+        "//aos/testing:path_rs",
+    ],
     visibility = ["//visibility:public"],
     deps = [
         ":configuration_rust_fbs",
diff --git a/aos/configuration.rs b/aos/configuration.rs
index 6711d77..31a259a 100644
--- a/aos/configuration.rs
+++ b/aos/configuration.rs
@@ -168,10 +168,12 @@
     use super::*;
 
     use aos_flatbuffers::Flatbuffer;
+    use aos_testing_path::artifact_path;
 
     #[test]
     fn read_config() {
-        let config = read_config_from(Path::new("aos/testdata/config1.json")).unwrap();
+        let config =
+            read_config_from(&artifact_path(Path::new("aos/testdata/config1.json"))).unwrap();
         assert!(
             config
                 .message()
diff --git a/aos/events/BUILD b/aos/events/BUILD
index 7f9c008..d799000 100644
--- a/aos/events/BUILD
+++ b/aos/events/BUILD
@@ -250,7 +250,6 @@
         "//aos/testing:aos_rs",
         "@com_github_google_flatbuffers//rust",
         "@crate_index//:futures",
-        "@rules_rust//tools/runfiles",
     ],
 )
 
@@ -611,8 +610,8 @@
     test_deps = [
         ":ping_rust_fbs",
         "//aos:test_init_rs",
+        "//aos/testing:path_rs",
         "@crate_index//:futures",
-        "@rules_rust//tools/runfiles",
     ],
     visibility = ["//visibility:public"],
     deps = [
@@ -644,8 +643,8 @@
     test_deps = [
         ":ping_rust_fbs",
         "//aos:test_init_rs",
+        "//aos/testing:path_rs",
         "@crate_index//:futures",
-        "@rules_rust//tools/runfiles",
     ],
     visibility = ["//visibility:public"],
     deps = [
diff --git a/aos/events/pingpong_test.rs b/aos/events/pingpong_test.rs
index 70d8772..be62642 100644
--- a/aos/events/pingpong_test.rs
+++ b/aos/events/pingpong_test.rs
@@ -1,15 +1,16 @@
 #[cfg(test)]
 mod tests {
+    use std::path::Path;
     use std::{cell::Cell, time::Duration};
 
     use aos::configuration::read_config_from;
     use aos::events::event_loop_runtime::{EventLoopRuntimeHolder, Watcher};
     use aos::events::simulated_event_loop::{SimulatedEventLoopFactory, SimulatedEventLoopHolder};
     use aos::testing::init::test_init;
+    use aos::testing::path::artifact_path;
     use ping_lib::PingTask;
     use ping_rust_fbs::aos::examples as ping;
     use pong_rust_fbs::aos::examples as pong;
-    use runfiles::Runfiles;
 
     // We use this trait to simplify leaking memory. For now, the event loop only allows
     // data with a `'static` lifetime. Until that restriction is lifted, we may leak
@@ -32,9 +33,8 @@
     impl PingPongTest {
         pub fn init() -> PingPongTest {
             test_init();
-            let r = Runfiles::create().unwrap();
             let config =
-                read_config_from(&r.rlocation("org_frc971/aos/events/pingpong_config.json"))
+                read_config_from(&artifact_path(Path::new("aos/events/pingpong_config.json")))
                     .unwrap()
                     .leak();
             let mut event_loop_factory = SimulatedEventLoopFactory::new(config);
diff --git a/aos/events/shm_event_loop.rs b/aos/events/shm_event_loop.rs
index ee6df18..5438045 100644
--- a/aos/events/shm_event_loop.rs
+++ b/aos/events/shm_event_loop.rs
@@ -233,12 +233,12 @@
 #[cfg(test)]
 mod tests {
     use super::*;
-
-    use runfiles::Runfiles;
+    use std::path::Path;
 
     use aos_configuration::read_config_from;
     use aos_events_event_loop_runtime::{Sender, Watcher};
     use aos_test_init::test_init;
+    use aos_testing_path::artifact_path;
     use ping_rust_fbs::aos::examples as ping;
     use std::borrow::Borrow;
     use std::sync::atomic::{AtomicUsize, Ordering};
@@ -249,9 +249,8 @@
     fn smoke_test() {
         test_init();
 
-        let r = Runfiles::create().unwrap();
         let config =
-            read_config_from(&r.rlocation("org_frc971/aos/events/pingpong_config.json")).unwrap();
+            read_config_from(&artifact_path(Path::new("aos/events/pingpong_config.json"))).unwrap();
 
         const VALUE: i32 = 971;
         let barrier = Barrier::new(2);
diff --git a/aos/events/simulated_event_loop.rs b/aos/events/simulated_event_loop.rs
index c75f34e..d47ec24 100644
--- a/aos/events/simulated_event_loop.rs
+++ b/aos/events/simulated_event_loop.rs
@@ -148,10 +148,11 @@
     use std::cell::RefCell;
 
     use futures::future::pending;
-    use runfiles::Runfiles;
+    use std::path::Path;
 
     use aos_configuration::read_config_from;
     use aos_test_init::test_init;
+    use aos_testing_path::artifact_path;
     use ping_rust_fbs::aos::examples::PingBuilder;
 
     // A really basic test of the functionality here.
@@ -166,10 +167,9 @@
         thread_local!(static GLOBAL_STATE: RefCell<GlobalState> = Default::default());
 
         test_init();
-        let r = Runfiles::create().unwrap();
-        let config = read_config_from(
-            &r.rlocation("org_frc971/aos/events/multinode_pingpong_test_combined_config.json"),
-        )
+        let config = read_config_from(&artifact_path(Path::new(
+            "aos/events/multinode_pingpong_test_combined_config.json",
+        )))
         .unwrap();
         let mut event_loop_factory = SimulatedEventLoopFactory::new(&config);
         {
diff --git a/aos/testing/BUILD b/aos/testing/BUILD
index eb9e2b2..729c978 100644
--- a/aos/testing/BUILD
+++ b/aos/testing/BUILD
@@ -1,3 +1,4 @@
+load("//tools/build_rules:clean_dep.bzl", "aos_repo_name")
 load("//tools/rust:defs.bzl", "rust_library")
 
 cc_library(
@@ -127,6 +128,7 @@
     name = "path",
     srcs = ["path.cc"],
     hdrs = ["path.h"],
+    local_defines = ["AOS_REPO_NAME=\\\"" + aos_repo_name() + "\\\""],
     target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
     deps = [
@@ -135,6 +137,14 @@
 )
 
 rust_library(
+    name = "path_rs",
+    srcs = ["path.rs"],
+    crate_name = "aos_testing_path",
+    rustc_env = {"AOS_REPO_NAME": aos_repo_name()},
+    visibility = ["//visibility:public"],
+)
+
+rust_library(
     name = "aos_rs",
     testonly = True,
     srcs = ["aos.rs"],
@@ -143,6 +153,7 @@
     gen_doctests = False,
     visibility = ["//visibility:public"],
     deps = [
+        ":path_rs",
         "//aos:aos_rs",
         "//aos:test_init_rs",
     ],
diff --git a/aos/testing/aos.rs b/aos/testing/aos.rs
index 6c7795d..b9e4264 100644
--- a/aos/testing/aos.rs
+++ b/aos/testing/aos.rs
@@ -4,4 +4,5 @@
 /// Utilities for testing an AOS application.
 pub mod testing {
     pub use aos_test_init as init;
+    pub use aos_testing_path as path;
 }
diff --git a/aos/testing/path.cc b/aos/testing/path.cc
index cb17c73..b8c4336 100644
--- a/aos/testing/path.cc
+++ b/aos/testing/path.cc
@@ -7,9 +7,7 @@
 // Returns the path to the provided artifact which works when built both as an
 // external target and in the repo.
 std::string ArtifactPath(std::string_view path) {
-  // TODO(austin): Don't hard-code the repo name here since it likely will
-  // change.
-  return absl::StrCat("../org_frc971/", path);
+  return absl::StrCat("../" AOS_REPO_NAME "/", path);
 }
 
 }  // namespace aos::testing
diff --git a/aos/testing/path.rs b/aos/testing/path.rs
new file mode 100644
index 0000000..c8da1e9
--- /dev/null
+++ b/aos/testing/path.rs
@@ -0,0 +1,9 @@
+use std::path::{Path, PathBuf};
+
+/// Returns the correct path for a data file within AOS, accounting for whether AOS is
+/// imported as an external repository.
+pub fn artifact_path(path: &Path) -> PathBuf {
+    Path::new("..")
+        .join(Path::new(env!("AOS_REPO_NAME")))
+        .join(path)
+}