forks/sorcery
git clone https://git.gae.moe/forks/sorcery
f9138f5
grace-patches
1//! Client for sorceryd's control socket (see `daemon/control.rs` there). 2 3use std:: io::{ BufRead , BufReader , Write }; 4use std:: os:: unix:: net:: UnixStream ; 5use std:: path:: Path ; 6 7use anyhow::{ Context , Result , bail}; 8 9/// Ask sorceryd to rebuild the site for the repository at `repo` now. 10pub fn send ( socket : & Path , repo : & Path ) ->Result <()> { 11let mut stream =UnixStream :: connect ( socket) 12. with_context ( ||format! ( "connecting to sorcery control socket {}" , socket. display ())) ?; 13writeln! ( stream, "{}" , repo. display ()) ?; 14let mut reply =String :: new (); 15BufReader :: new ( stream). read_line ( & mut reply) ?; 16if reply. split_whitespace (). next () !=Some ( "ok" ) { 17bail! ( "sorcery replied {:?}" , reply. trim_end ()); 18} 19Ok (()) 20} 21 22# [ cfg ( test )] 23mod tests{ 24use std:: os:: unix:: net:: UnixListener ; 25use std:: thread; 26 27use super :: * ; 28 29fn serve_once ( socket : & Path , reply : &' static str ) -> thread:: JoinHandle < String > { 30let listener =UnixListener :: bind ( socket). unwrap (); 31 thread:: spawn ( move ||{ 32let ( mut connection, _) = listener. accept (). unwrap (); 33let mut request =String :: new (); 34BufReader :: new ( connection. try_clone (). unwrap ()) 35. read_line ( & mut request) 36. unwrap (); 37 connection. write_all ( reply. as_bytes ()). unwrap (); 38 request 39}) 40} 41 42# [ test ] 43fn sends_repository_path_and_interprets_reply () { 44let temp = tempfile:: tempdir (). unwrap (); 45let repo =Path :: new ( "/home/git/public/alice/a repo" ); 46 47let server =serve_once ( 48& temp. path (). join ( "ok" ), 49"ok https://git.example/alice/a%20repo\n" , 50); 51send ( & temp. path (). join ( "ok" ), repo). unwrap (); 52assert_eq! ( server. join (). unwrap (), "/home/git/public/alice/a repo\n" ); 53 54let server =serve_once ( & temp. path (). join ( "bare" ), "ok\n" ); 55send ( & temp. path (). join ( "bare" ), repo). unwrap (); 56 server. join (). unwrap (); 57 58let server =serve_once ( & temp. path (). join ( "error" ), "error nope\n" ); 59let error =send ( & temp. path (). join ( "error" ), repo). unwrap_err (); 60 server. join (). unwrap (); 61assert! ( error. to_string (). contains ( "nope" )); 62} 63}