|
| 1 | +use std::io::Write; |
| 2 | + |
| 3 | +use amalthea::fixtures::dummy_frontend::ExecuteRequestOptions; |
| 4 | +use ark::fixtures::DummyArkFrontendRprofile; |
| 5 | + |
| 6 | +// You must run these tests with `cargo nextest` because they initialise |
| 7 | +// incompatible process singletons |
| 8 | + |
| 9 | +/// See https://github.com/posit-dev/positron/issues/4973 |
| 10 | +#[test] |
| 11 | +fn test_r_profile_can_cat() { |
| 12 | + let message = "hi from rprofile"; |
| 13 | + |
| 14 | + // The `\n` is critical, otherwise R's `source()` silently fails |
| 15 | + let contents = format!("cat('{message}')\n"); |
| 16 | + |
| 17 | + // Write `contents` to a tempfile that we declare to be |
| 18 | + // the `.Rprofile` that R should use |
| 19 | + let mut file = tempfile::NamedTempFile::new().unwrap(); |
| 20 | + write!(file, "{contents}").unwrap(); |
| 21 | + |
| 22 | + let path = file.path(); |
| 23 | + let path = path.to_str().unwrap(); |
| 24 | + |
| 25 | + unsafe { std::env::set_var("R_PROFILE_USER", path) }; |
| 26 | + |
| 27 | + // Ok, load up R now. It should `cat()` the `message` over iopub. |
| 28 | + let frontend = DummyArkFrontendRprofile::lock(); |
| 29 | + |
| 30 | + frontend.recv_iopub_stream_stdout(message) |
| 31 | +} |
| 32 | + |
| 33 | +/// See https://github.com/posit-dev/positron/issues/4253 |
| 34 | +#[test] |
| 35 | +fn test_r_profile_is_only_run_once() { |
| 36 | + // The trailing `\n` is critical, otherwise R's `source()` silently fails |
| 37 | + let contents = r#" |
| 38 | +if (exists("x")) { |
| 39 | + x <- 2 |
| 40 | +} else { |
| 41 | + x <- 1 |
| 42 | +} |
| 43 | +
|
| 44 | +"#; |
| 45 | + |
| 46 | + // Write `contents` to a tempfile that we declare to be |
| 47 | + // the `.Rprofile` that R should use |
| 48 | + let mut file = tempfile::NamedTempFile::new().unwrap(); |
| 49 | + write!(file, "{contents}").unwrap(); |
| 50 | + |
| 51 | + let path = file.path(); |
| 52 | + let path = path.to_str().unwrap(); |
| 53 | + |
| 54 | + unsafe { std::env::set_var("R_PROFILE_USER", path) }; |
| 55 | + |
| 56 | + // Ok, start R. If we've set everything correctly, R should not run |
| 57 | + // the `.Rprofile`, but ark should - i.e. it should run exactly 1 time. |
| 58 | + let frontend = DummyArkFrontendRprofile::lock(); |
| 59 | + |
| 60 | + frontend.send_execute_request("x", ExecuteRequestOptions::default()); |
| 61 | + frontend.recv_iopub_busy(); |
| 62 | + |
| 63 | + let input = frontend.recv_iopub_execute_input(); |
| 64 | + assert_eq!(input.code, "x"); |
| 65 | + assert_eq!(frontend.recv_iopub_execute_result(), "[1] 1"); |
| 66 | + |
| 67 | + frontend.recv_iopub_idle(); |
| 68 | + |
| 69 | + assert_eq!(frontend.recv_shell_execute_reply(), input.execution_count); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_missing_user_profile() { |
| 74 | + std::env::set_var("R_PROFILE_USER", "~/does/not/exist"); |
| 75 | + |
| 76 | + let frontend = DummyArkFrontendRprofile::lock(); |
| 77 | + |
| 78 | + frontend.send_execute_request("1", ExecuteRequestOptions::default()); |
| 79 | + frontend.recv_iopub_busy(); |
| 80 | + |
| 81 | + let input = frontend.recv_iopub_execute_input(); |
| 82 | + assert_eq!(input.code, "1"); |
| 83 | + assert_eq!(frontend.recv_iopub_execute_result(), "[1] 1"); |
| 84 | + |
| 85 | + frontend.recv_iopub_idle(); |
| 86 | + |
| 87 | + assert_eq!(frontend.recv_shell_execute_reply(), input.execution_count); |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn test_missing_site_profile() { |
| 92 | + std::env::set_var("R_PROFILE", "~/does/not/exist"); |
| 93 | + |
| 94 | + let frontend = DummyArkFrontendRprofile::lock(); |
| 95 | + |
| 96 | + frontend.send_execute_request("1", ExecuteRequestOptions::default()); |
| 97 | + frontend.recv_iopub_busy(); |
| 98 | + |
| 99 | + let input = frontend.recv_iopub_execute_input(); |
| 100 | + assert_eq!(input.code, "1"); |
| 101 | + assert_eq!(frontend.recv_iopub_execute_result(), "[1] 1"); |
| 102 | + |
| 103 | + frontend.recv_iopub_idle(); |
| 104 | + |
| 105 | + assert_eq!(frontend.recv_shell_execute_reply(), input.execution_count); |
| 106 | +} |
0 commit comments