char/sorcery
static-files based git repo viewer
git clone https://git.t4t.associates/char/sorcery
4a707c3
main
12use std :: fs ; 3use std :: io :: { BufRead , BufReader , Read , Write } ; 4use std :: os :: unix :: net :: UnixStream ; 5use std :: path :: { Path , PathBuf } ; 6use std :: process :: { Child , Command , Stdio } ; 7use std :: time :: { Duration , Instant , SystemTime , UNIX_EPOCH } ; 89use anyhow :: { Context , Result , bail } ; 1011struct TestDir ( PathBuf ) ; 1213impl TestDir { 14fn new () -> Result < Self > { 15// long TMPDIRs like unsandboxed Nix build dirs 16// can exceed max path size limits so we'll use /tmp here 17// if we'd overshoot, but use the regular tmpdir instead 18let temp = std :: env :: temp_dir () ; 19let base = if temp . as_os_str () . len () > 72 && Path :: new ( "/tmp" ) . is_dir () { 20PathBuf :: from ( "/tmp" ) 21} else { 22temp 23} ; 24let pid = std :: process :: id () ; 25for attempt in 0u32 .. 64 { 26let nanos = SystemTime :: now () . duration_since ( UNIX_EPOCH ) ?. subsec_nanos () ; 27let path = base . join ( format ! ( "s-{pid:x}-{:x}" , nanos . wrapping_add ( attempt ))) ; 28match fs :: create_dir ( & path ) { 29Ok (()) => return Ok ( Self ( path )) , 30Err ( error ) if error . kind () == std :: io :: ErrorKind :: AlreadyExists => continue , 31Err ( error ) => return Err ( error . into ()) , 32} 33} 34bail ! ( "could not create test directory in {}" , base . display ()) ; 35} 36} 3738impl Drop for TestDir { 39fn drop ( & mut self ) { 40let _ = fs :: remove_dir_all ( & self . 0 ) ; 41} 42} 4344struct Daemon ( Child ) ; 4546impl Drop for Daemon { 47fn drop ( & mut self ) { 48if self . 0 . try_wait () . ok () . flatten () . is_none () { 49let _ = Command :: new ( "kill" ) 50. arg ( "-TERM" ) 51. arg ( self . 0 . id () . to_string ()) 52. status () ; 53let _ = self . 0 . wait () ; 54} 55} 56} 5758fn git ( repo : & Path , args : & [ & str ]) -> Result < () > { 59let status = Command :: new ( "git" ) 60. arg ( "-C" ) 61. arg ( repo ) 62. args ( args ) 63. env ( "GIT_AUTHOR_NAME" , "test" ) 64. env ( "GIT_AUTHOR_EMAIL" , "test@example.com" ) 65. env ( "GIT_COMMITTER_NAME" , "test" ) 66. env ( "GIT_COMMITTER_EMAIL" , "test@example.com" ) 67. stdout ( Stdio :: null ()) 68. status () ? ; 69if ! status . success () { 70bail ! ( "git {args:?} exited with {status}" ) ; 71} 72Ok (()) 73} 7475fn refresh ( socket : & Path , path : & Path ) -> Result < String > { 76let mut stream = UnixStream :: connect ( socket ) ? ; 77stream . set_read_timeout ( Some ( Duration :: from_secs ( 30 ))) ? ; 78writeln ! ( stream , "{}" , path . display ()) ? ; 79let mut reply = String :: new () ; 80BufReader :: new ( stream ) . read_line ( & mut reply ) ? ; 81Ok ( reply ) 82} 8384fn http ( socket : & Path , path : & str ) -> Result < String > { 85let mut stream = UnixStream :: connect ( socket ) ? ; 86stream . set_read_timeout ( Some ( Duration :: from_secs ( 10 ))) ? ; 87write ! ( 88stream , 89"GET {path} HTTP/1.1 \r\n Host: localhost \r\n Connection: close \r\n\r\n " 90) ? ; 91let mut response = String :: new () ; 92stream . read_to_string ( & mut response ) ? ; 93Ok ( response ) 94} 9596#[ test ] 97fn refreshes_published_repositories_and_reports_their_urls () -> Result < () > { 98let root = TestDir :: new () ? ; 99let repo = root . 0 . join ( "repos/alice/thing" ) ; 100fs :: create_dir_all ( & repo ) ? ; 101git ( & repo , & [ "init" , "-q" , "-b" , "main" ]) ? ; 102fs :: write ( repo . join ( "README" ) , "hello" ) ? ; 103git ( & repo , & [ "add" , "README" ]) ? ; 104git ( & repo , & [ "commit" , "-q" , "-m" , "initial" ]) ? ; 105106let socket = root . 0 . join ( "http.sock" ) ; 107let control = root . 0 . join ( "control.sock" ) ; 108let cache = root . 0 . join ( "cache" ) ; 109let mut command = Command :: new ( env ! ( "CARGO_BIN_EXE_sorceryd" )) ; 110for ( key , _ ) in std :: env :: vars_os () { 111if key . to_string_lossy () . starts_with ( "SORCERY_" ) { 112command . env_remove ( key ) ; 113} 114} 115let mut daemon = Daemon ( 116command 117. arg ( "serve" ) 118. arg ( "--config" ) 119. arg ( "/dev/null" ) 120. arg ( "--repositories" ) 121. arg ( root . 0 . join ( "repos" )) 122. arg ( "--cache" ) 123. arg ( & cache ) 124. arg ( "--socket" ) 125. arg ( & socket ) 126. arg ( "--control-socket" ) 127. arg ( & control ) 128. arg ( "--instance-name=test" ) 129. arg ( "--clone-url-base=https://git.example" ) 130. arg ( "--x-accel=false" ) 131. stdout ( Stdio :: null ()) 132. spawn () ? , 133) ; 134let deadline = Instant :: now () + Duration :: from_secs ( 30 ) ; 135while ! socket . exists () || ! control . exists () { 136if let Some ( status ) = daemon . 0 . try_wait () ? { 137bail ! ( "daemon exited with {status}" ) ; 138} 139if Instant :: now () >= deadline { 140bail ! ( "daemon sockets did not appear" ) ; 141} 142std :: thread :: sleep ( Duration :: from_millis ( 20 )) ; 143} 144assert_eq ! ( 145refresh ( & control , & repo ) ? , 146"ok https://git.example/alice/thing \n " 147) ; 148assert_eq ! ( refresh ( & control , & root . 0 . join ( "repos/alice" )) ? , "ok \n " ) ; 149assert ! ( refresh ( & control , & root . 0 . join ( "missing" )) ?. starts_with ( "error " )) ; 150assert ! ( cache . join ( "alice/thing/.sorcery-state" ) . is_file ()) ; 151let page = http ( & socket , "/alice/thing/" ) ? ; 152assert ! ( page . starts_with ( "HTTP/1.1 200" ) , "{page}" ) ; 153assert ! ( page . contains ( "alice/thing" )) ; 154let health = http ( & socket , "/-/health" ) ? ; 155assert ! ( 156health . starts_with ( "HTTP/1.1 200" ) && health . contains ( "ok \n " ) , 157"{health}" 158) ; 159160let status = Command :: new ( "kill" ) 161. arg ( "-TERM" ) 162. arg ( daemon . 0 . id () . to_string ()) 163. status () ? ; 164assert ! ( status . success ()) ; 165let status = daemon . 0 . wait () . context ( "waiting for daemon shutdown" ) ? ; 166assert ! ( status . success () , "daemon exited with {status}" ) ; 167assert ! ( ! socket . exists () && ! control . exists ()) ; 168Ok (()) }