|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::test_utils::*; |
| 19 | +use anyhow::Result; |
| 20 | +use std::fs; |
| 21 | +use std::io::Write; |
| 22 | +use tempfile::TempDir; |
| 23 | + |
| 24 | +#[tokio::test] |
| 25 | +async fn test_tee_destination_already_exists() -> Result<()> { |
| 26 | + let temp_dir = TempDir::new()?; |
| 27 | + let dest_path = temp_dir.path().join("dest.txt"); |
| 28 | + |
| 29 | + let source_content = "Source content"; |
| 30 | + let initial_dest_content = "Initial dest content"; |
| 31 | + |
| 32 | + fs::write(&dest_path, initial_dest_content)?; |
| 33 | + |
| 34 | + let mut cmd = oli(); |
| 35 | + cmd.arg("tee").arg(dest_path.to_str().unwrap()); |
| 36 | + |
| 37 | + cmd.stdin(std::process::Stdio::piped()); |
| 38 | + cmd.stdout(std::process::Stdio::piped()); |
| 39 | + let mut child = cmd.spawn()?; |
| 40 | + let mut stdin = child.stdin.take().expect("Failed to open stdin"); |
| 41 | + |
| 42 | + let content_to_write = source_content.to_string(); |
| 43 | + std::thread::spawn(move || { |
| 44 | + stdin |
| 45 | + .write_all(content_to_write.as_bytes()) |
| 46 | + .expect("Failed to write to stdin"); |
| 47 | + }); |
| 48 | + |
| 49 | + let output = child.wait_with_output()?; |
| 50 | + assert!(output.status.success()); |
| 51 | + |
| 52 | + let stdout_output = String::from_utf8(output.stdout.clone())?; |
| 53 | + assert_eq!(stdout_output, source_content); |
| 54 | + let dest_content_after_tee = fs::read_to_string(&dest_path)?; |
| 55 | + assert_eq!(dest_content_after_tee, source_content); |
| 56 | + |
| 57 | + Ok(()) |
| 58 | +} |
| 59 | + |
| 60 | +#[tokio::test] |
| 61 | +async fn test_tee_stdin() -> Result<()> { |
| 62 | + let temp_dir = TempDir::new()?; |
| 63 | + let dest_path = temp_dir.path().join("dest_stdin.txt"); |
| 64 | + |
| 65 | + let test_content = "Hello from stdin!"; |
| 66 | + |
| 67 | + let mut cmd = oli(); |
| 68 | + cmd.arg("tee").arg(&dest_path); |
| 69 | + |
| 70 | + cmd.stdin(std::process::Stdio::piped()); |
| 71 | + cmd.stdout(std::process::Stdio::piped()); |
| 72 | + let mut child = cmd.spawn()?; |
| 73 | + let mut stdin = child.stdin.take().expect("Failed to open stdin"); |
| 74 | + |
| 75 | + let content_to_write = test_content.to_string(); |
| 76 | + std::thread::spawn(move || { |
| 77 | + stdin |
| 78 | + .write_all(content_to_write.as_bytes()) |
| 79 | + .expect("Failed to write to stdin"); |
| 80 | + }); |
| 81 | + |
| 82 | + let output = child.wait_with_output()?; |
| 83 | + |
| 84 | + assert!(output.status.success()); |
| 85 | + |
| 86 | + let stdout_output = String::from_utf8(output.stdout.clone())?; |
| 87 | + assert_eq!(stdout_output, test_content); |
| 88 | + let dest_content = fs::read_to_string(&dest_path)?; |
| 89 | + assert_eq!(dest_content, test_content); |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
| 93 | + |
| 94 | +#[test] |
| 95 | +fn test_tee_non_existing_file() -> Result<()> { |
| 96 | + let temp_dir = TempDir::new()?; |
| 97 | + let dst_path = temp_dir.path().join("non_existing_file.txt"); |
| 98 | + let dst_path_str = dst_path.as_os_str().to_str().unwrap(); |
| 99 | + |
| 100 | + let mut cmd: assert_cmd::Command = std::process::Command::cargo_bin("oli")?.into(); |
| 101 | + cmd.args(["tee", dst_path_str]); |
| 102 | + cmd.write_stdin("Hello, world!"); |
| 103 | + cmd.assert().success(); |
| 104 | + |
| 105 | + let content = fs::read_to_string(dst_path)?; |
| 106 | + assert_eq!(content, "Hello, world!"); |
| 107 | + |
| 108 | + Ok(()) |
| 109 | +} |
| 110 | + |
| 111 | +#[test] |
| 112 | +fn test_tee_append_succeed() -> Result<()> { |
| 113 | + let temp_dir = TempDir::new()?; |
| 114 | + let dst_path = temp_dir.path().join("test_append.txt"); |
| 115 | + let dst_path_str = dst_path.as_os_str().to_str().unwrap(); |
| 116 | + |
| 117 | + // Initial content |
| 118 | + fs::write(&dst_path, "Hello, ")?; |
| 119 | + |
| 120 | + let mut cmd: assert_cmd::Command = std::process::Command::cargo_bin("oli")?.into(); |
| 121 | + cmd.args(["tee", "-a", dst_path_str]); |
| 122 | + cmd.write_stdin("world!"); |
| 123 | + cmd.assert().success(); |
| 124 | + |
| 125 | + let content = fs::read_to_string(dst_path)?; |
| 126 | + assert_eq!(content, "Hello, world!"); |
| 127 | + |
| 128 | + Ok(()) |
| 129 | +} |
| 130 | + |
| 131 | +#[test] |
| 132 | +fn test_tee_append_file_not_found() -> Result<()> { |
| 133 | + let temp_dir = TempDir::new()?; |
| 134 | + let file_path = temp_dir.path().join("test_append_not_found.txt"); |
| 135 | + let file_path_str = file_path.to_str().unwrap(); |
| 136 | + |
| 137 | + let mut cmd: assert_cmd::Command = std::process::Command::cargo_bin("oli")?.into(); |
| 138 | + cmd.arg("tee") |
| 139 | + .arg("-a") |
| 140 | + .arg(file_path_str) |
| 141 | + .write_stdin("append data") |
| 142 | + .assert() |
| 143 | + .success(); |
| 144 | + |
| 145 | + let content = fs::read_to_string(file_path)?; |
| 146 | + assert_eq!(content, "append data"); |
| 147 | + |
| 148 | + Ok(()) |
| 149 | +} |
| 150 | + |
| 151 | +#[test] |
| 152 | +fn test_tee_overwrite_existing_file() -> Result<()> { |
| 153 | + let temp_dir = TempDir::new()?; |
| 154 | + let file_path = temp_dir.path().join("test_overwrite.txt"); |
| 155 | + let file_path_str = file_path.to_str().unwrap(); |
| 156 | + |
| 157 | + // Create an existing file with some content |
| 158 | + fs::write(&file_path, "initial data")?; |
| 159 | + |
| 160 | + let mut cmd: assert_cmd::Command = std::process::Command::cargo_bin("oli")?.into(); |
| 161 | + cmd.arg("tee").arg(file_path_str).write_stdin("new data"); |
| 162 | + cmd.assert().success(); |
| 163 | + |
| 164 | + let content = fs::read_to_string(file_path)?; |
| 165 | + assert_eq!(content, "new data"); |
| 166 | + |
| 167 | + Ok(()) |
| 168 | +} |
0 commit comments