annotate rust/chg/src/message.rs @ 48178:f12a19d03d2c

fix: reduce number of tool executions By grouping together (path, ctx) pairs according to the inputs they would provide to fixer tools, we can deduplicate executions of fixer tools to significantly reduce the amount of time spent running slow tools. This change does not handle clean files in the working copy, which could still be deduplicated against the files in the checked out commit. It's a little harder to do that because the filerev is not available in the workingfilectx (and it doesn't exist for added files). Anecdotally, this change makes some real uses cases at Google 10x faster. I think we were originally hesitant to do this because the benefits weren't obvious, and implementing it efficiently is kind of tricky. If we simply memoized the formatter execution function, we would be keeping tons of file content in memory. Also included is a regression test for a corner case that I broke with my first attempt at optimizing this code. Differential Revision: https://phab.mercurial-scm.org/D11280
author Danny Hooper <hooper@google.com>
date Thu, 02 Sep 2021 14:08:45 -0700
parents 426294d06ddc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org>
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
2 //
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
3 // This software may be used and distributed according to the terms of the
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
4 // GNU General Public License version 2 or any later version.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
5
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
6 //! Utility for parsing and building command-server messages.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
7
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
8 use bytes::{BufMut, Bytes, BytesMut};
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
9 use std::error;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
10 use std::ffi::{OsStr, OsString};
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
11 use std::io;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
12 use std::os::unix::ffi::OsStrExt;
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
13 use std::path::PathBuf;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
14
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
15 pub use tokio_hglib::message::*; // re-exports
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
16
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
17 /// Shell command type requested by the server.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
18 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
19 pub enum CommandType {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
20 /// Pager should be spawned.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
21 Pager,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
22 /// Shell command should be executed to send back the result code.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
23 System,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
24 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
25
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
26 /// Shell command requested by the server.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
27 #[derive(Clone, Debug, Eq, PartialEq)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
28 pub struct CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
29 pub command: OsString,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
30 pub current_dir: OsString,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
31 pub envs: Vec<(OsString, OsString)>,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
32 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
33
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
34 /// Parses "S" channel request into command type and spec.
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
35 pub fn parse_command_spec(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
36 data: Bytes,
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
37 ) -> io::Result<(CommandType, CommandSpec)> {
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
38 let mut split = data.split(|&c| c == b'\0');
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
39 let ctype = parse_command_type(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
40 split.next().ok_or(new_parse_error("missing type"))?,
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
41 )?;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
42 let command = split.next().ok_or(new_parse_error("missing command"))?;
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
43 let current_dir =
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
44 split.next().ok_or(new_parse_error("missing current dir"))?;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
45
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
46 let mut envs = Vec::new();
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
47 for l in split {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
48 let mut s = l.splitn(2, |&c| c == b'=');
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
49 let k = s.next().unwrap();
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
50 let v = s.next().ok_or(new_parse_error("malformed env"))?;
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
51 envs.push((
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
52 OsStr::from_bytes(k).to_owned(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
53 OsStr::from_bytes(v).to_owned(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
54 ));
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
55 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
56
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
57 let spec = CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
58 command: OsStr::from_bytes(command).to_owned(),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
59 current_dir: OsStr::from_bytes(current_dir).to_owned(),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
60 envs: envs,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
61 };
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
62 Ok((ctype, spec))
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
63 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
64
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
65 fn parse_command_type(value: &[u8]) -> io::Result<CommandType> {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
66 match value {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
67 b"pager" => Ok(CommandType::Pager),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
68 b"system" => Ok(CommandType::System),
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
69 _ => Err(new_parse_error(format!(
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
70 "unknown command type: {}",
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
71 decode_latin1(value)
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
72 ))),
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
73 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
74 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
75
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
76 /// Client-side instruction requested by the server.
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
77 #[derive(Clone, Debug, Eq, PartialEq)]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
78 pub enum Instruction {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
79 Exit(i32),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
80 Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
81 Redirect(PathBuf),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
82 Unlink(PathBuf),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
83 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
84
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
85 /// Parses validation result into instructions.
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
86 pub fn parse_instructions(data: Bytes) -> io::Result<Vec<Instruction>> {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
87 let mut instructions = Vec::new();
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
88 for l in data.split(|&c| c == b'\0') {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
89 if l.is_empty() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
90 continue;
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
91 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
92 let mut s = l.splitn(2, |&c| c == b' ');
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
93 let inst = match (s.next().unwrap(), s.next()) {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
94 (b"exit", Some(arg)) => decode_latin1(arg)
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
95 .parse()
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
96 .map(Instruction::Exit)
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
97 .map_err(|_| {
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
98 new_parse_error(format!("invalid exit code: {:?}", arg))
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
99 })?,
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
100 (b"reconnect", None) => Instruction::Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
101 (b"redirect", Some(arg)) => {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
102 Instruction::Redirect(OsStr::from_bytes(arg).to_owned().into())
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
103 }
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
104 (b"unlink", Some(arg)) => {
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
105 Instruction::Unlink(OsStr::from_bytes(arg).to_owned().into())
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
106 }
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
107 _ => {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
108 return Err(new_parse_error(format!(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
109 "unknown command: {:?}",
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
110 l
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
111 )));
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
112 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
113 };
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
114 instructions.push(inst);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
115 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
116 Ok(instructions)
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
117 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
118
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
119 // allocate large buffer as environment variables can be quite long
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
120 const INITIAL_PACKED_ENV_VARS_CAPACITY: usize = 4096;
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
121
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
122 /// Packs environment variables of platform encoding into bytes.
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
123 ///
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
124 /// # Panics
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
125 ///
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
126 /// Panics if key or value contains `\0` character, or key contains '='
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
127 /// character.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
128 pub fn pack_env_vars_os(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
129 vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
130 ) -> Bytes {
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
131 let mut vars_iter = vars.into_iter();
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
132 if let Some((k, v)) = vars_iter.next() {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
133 let mut dst =
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
134 BytesMut::with_capacity(INITIAL_PACKED_ENV_VARS_CAPACITY);
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
135 pack_env_into(&mut dst, k.as_ref(), v.as_ref());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
136 for (k, v) in vars_iter {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
137 dst.reserve(1);
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
138 dst.put_u8(b'\0');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
139 pack_env_into(&mut dst, k.as_ref(), v.as_ref());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
140 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
141 dst.freeze()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
142 } else {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
143 Bytes::new()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
144 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
145 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
146
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
147 fn pack_env_into(dst: &mut BytesMut, k: &OsStr, v: &OsStr) {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
148 assert!(!k.as_bytes().contains(&0), "key shouldn't contain NUL");
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
149 assert!(!k.as_bytes().contains(&b'='), "key shouldn't contain '='");
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
150 assert!(!v.as_bytes().contains(&0), "value shouldn't contain NUL");
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
151 dst.reserve(k.as_bytes().len() + 1 + v.as_bytes().len());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
152 dst.put_slice(k.as_bytes());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
153 dst.put_u8(b'=');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
154 dst.put_slice(v.as_bytes());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
155 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
156
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
157 fn decode_latin1(s: impl AsRef<[u8]>) -> String {
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
158 s.as_ref().iter().map(|&c| c as char).collect()
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
159 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
160
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
161 fn new_parse_error(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
162 error: impl Into<Box<dyn error::Error + Send + Sync>>,
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
163 ) -> io::Error {
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
164 io::Error::new(io::ErrorKind::InvalidData, error)
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
165 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
166
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
167 #[cfg(test)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
168 mod tests {
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
169 use super::*;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
170 use std::os::unix::ffi::OsStringExt;
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
171 use std::panic;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
172
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
173 #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
174 fn parse_command_spec_good() {
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
175 let src = [
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
176 b"pager".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
177 b"less -FRX".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
178 b"/tmp".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
179 b"LANG=C".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
180 b"HGPLAIN=".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
181 ]
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
182 .join(&0);
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
183 let spec = CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
184 command: os_string_from(b"less -FRX"),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
185 current_dir: os_string_from(b"/tmp"),
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
186 envs: vec![
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
187 (os_string_from(b"LANG"), os_string_from(b"C")),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
188 (os_string_from(b"HGPLAIN"), os_string_from(b"")),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
189 ],
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
190 };
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
191 assert_eq!(
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
192 parse_command_spec(Bytes::from(src)).unwrap(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
193 (CommandType::Pager, spec)
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
194 );
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
195 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
196
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
197 #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
198 fn parse_command_spec_too_short() {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
199 assert!(parse_command_spec(Bytes::from_static(b"")).is_err());
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
200 assert!(parse_command_spec(Bytes::from_static(b"pager")).is_err());
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
201 assert!(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
202 parse_command_spec(Bytes::from_static(b"pager\0less")).is_err()
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
203 );
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
204 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
205
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
206 #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
207 fn parse_command_spec_malformed_env() {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
208 assert!(parse_command_spec(Bytes::from_static(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
209 b"pager\0less\0/tmp\0HOME"
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
210 ))
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
211 .is_err());
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
212 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
213
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
214 #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
215 fn parse_command_spec_unknown_type() {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
216 assert!(
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
217 parse_command_spec(Bytes::from_static(b"paper\0less")).is_err()
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44693
diff changeset
218 );
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
219 }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
220
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
221 #[test]
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
222 fn parse_instructions_good() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
223 let src = [
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
224 b"exit 123".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
225 b"reconnect".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
226 b"redirect /whatever".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
227 b"unlink /someother".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
228 ]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
229 .join(&0);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
230 let insts = vec![
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
231 Instruction::Exit(123),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
232 Instruction::Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
233 Instruction::Redirect(path_buf_from(b"/whatever")),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
234 Instruction::Unlink(path_buf_from(b"/someother")),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
235 ];
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
236 assert_eq!(parse_instructions(Bytes::from(src)).unwrap(), insts);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
237 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
238
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
239 #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
240 fn parse_instructions_empty() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
241 assert_eq!(parse_instructions(Bytes::new()).unwrap(), vec![]);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
242 assert_eq!(
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
243 parse_instructions(Bytes::from_static(b"\0")).unwrap(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
244 vec![]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
245 );
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
246 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
247
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
248 #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
249 fn parse_instructions_malformed_exit_code() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
250 assert!(parse_instructions(Bytes::from_static(b"exit foo")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
251 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
252
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
253 #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
254 fn parse_instructions_missing_argument() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
255 assert!(parse_instructions(Bytes::from_static(b"exit")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
256 assert!(parse_instructions(Bytes::from_static(b"redirect")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
257 assert!(parse_instructions(Bytes::from_static(b"unlink")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
258 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
259
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
260 #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
261 fn parse_instructions_unknown_command() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
262 assert!(parse_instructions(Bytes::from_static(b"quit 0")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
263 }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
264
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
265 #[test]
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
266 fn pack_env_vars_os_good() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
267 assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
268 pack_env_vars_os(vec![] as Vec<(OsString, OsString)>),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
269 Bytes::new()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
270 );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
271 assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
272 pack_env_vars_os(vec![os_string_pair_from(b"FOO", b"bar")]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
273 Bytes::from_static(b"FOO=bar")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
274 );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
275 assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
276 pack_env_vars_os(vec![
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
277 os_string_pair_from(b"FOO", b""),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
278 os_string_pair_from(b"BAR", b"baz")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
279 ]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
280 Bytes::from_static(b"FOO=\0BAR=baz")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
281 );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
282 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
283
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
284 #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
285 fn pack_env_vars_os_large_key() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
286 let mut buf = vec![b'A'; INITIAL_PACKED_ENV_VARS_CAPACITY];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
287 let envs = vec![os_string_pair_from(&buf, b"")];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
288 buf.push(b'=');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
289 assert_eq!(pack_env_vars_os(envs), Bytes::from(buf));
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
290 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
291
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
292 #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
293 fn pack_env_vars_os_large_value() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
294 let mut buf = vec![b'A', b'='];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
295 buf.resize(INITIAL_PACKED_ENV_VARS_CAPACITY + 1, b'a');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
296 let envs = vec![os_string_pair_from(&buf[..1], &buf[2..])];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
297 assert_eq!(pack_env_vars_os(envs), Bytes::from(buf));
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
298 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
299
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
300 #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
301 fn pack_env_vars_os_nul_eq() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
302 assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
303 pack_env_vars_os(vec![os_string_pair_from(b"\0", b"")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
304 })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
305 .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
306 assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
307 pack_env_vars_os(vec![os_string_pair_from(b"FOO", b"\0bar")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
308 })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
309 .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
310 assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
311 pack_env_vars_os(vec![os_string_pair_from(b"FO=", b"bar")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
312 })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
313 .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
314 assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
315 pack_env_vars_os(vec![os_string_pair_from(b"FOO", b"=ba")]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
316 Bytes::from_static(b"FOO==ba")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
317 );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
318 }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
319
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
320 fn os_string_from(s: &[u8]) -> OsString {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
321 OsString::from_vec(s.to_vec())
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
322 }
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
323
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
324 fn os_string_pair_from(k: &[u8], v: &[u8]) -> (OsString, OsString) {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
325 (os_string_from(k), os_string_from(v))
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
326 }
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
327
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
328 fn path_buf_from(s: &[u8]) -> PathBuf {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
329 os_string_from(s).into()
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
330 }
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
331 }