| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | | use crate::auth::roles; |
| 10 | | use crate::db::{self, projects as pdb}; |
| 11 | | use crate::repos::RepoName; |
| 12 | | use crate::Shared; |
| 13 | | use oxy_core::repo::FileEdit; |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | 29× | pub async fn check_name(app: &Shared, account: &db::accounts::Account, owner: &str, name: &str) -> Result<RepoName, String> { |
| 18 | 29× | let Some(n) = RepoName::new(owner.trim(), name.trim()) else { |
| 19 | 2× | return Err("A repository name is letters, digits, dots, dashes and underscores, up to 64.".into()); |
| 20 | | }; |
| 21 | 27× | if n.name.ends_with(crate::wiki::SUFFIX) { |
| 22 | 1× | return Err("A name cannot end in .wiki: that is the address of a repository's wiki.".into()); |
| 23 | 26× | } |
| 24 | 26× | if !roles::may_create_under(&app.db, account, &n.owner).await.map_err(|e| e.to_string())? { |
| 25 | 0× | return Err(format!("You cannot create repositories under {}.", n.owner)); |
| 26 | 26× | } |
| 27 | 26× | if db::repos::get(&app.db, &n.owner, &n.name).await.map_err(|e| e.to_string())?.is_some() || app.repos.exists(&n) { |
| 28 | 1× | return Err(format!("{n} exists.")); |
| 29 | 25× | } |
| 30 | 25× | Ok(n) |
| 31 | 29× | } |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 36 | | pub enum Board { |
| 37 | | None, |
| 38 | | New(String), |
| 39 | | Existing(i64), |
| 40 | | } |
| 41 | |
|
| 42 | | impl Board { |
| 43 | | |
| 44 | | |
| 45 | 29× | pub fn parse(choice: &str, title: &str, repo: &str) -> Result<Board, String> { |
| 46 | 29× | match choice.trim() { |
| 47 | 29× | "" | "none" => Ok(Board::None), |
| 48 | 5× | "new" => { |
| 49 | 3× | let t: String = title.trim().chars().take(100).collect(); |
| 50 | 3× | Ok(Board::New(if t.is_empty() { repo.to_string() } else { t })) |
| 51 | | } |
| 52 | 2× | n => n.parse().map(Board::Existing).map_err(|_| format!("No board {n}.")), |
| 53 | | } |
| 54 | 29× | } |
| 55 | | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 47× | pub async fn check_board(app: &Shared, account: &db::accounts::Account, owner: &str, board: &Board) -> Result<(), String> { |
| 60 | 47× | match board { |
| 61 | 43× | Board::None => Ok(()), |
| 62 | 2× | Board::New(_) => match crate::web::projects::rights_of(app, Some(account), owner).await.map_err(|e| e.to_string())? { |
| 63 | 2× | Some(r) if r.edit => Ok(()), |
| 64 | 0× | _ => Err(format!("You cannot make boards under {owner}.")), |
| 65 | | }, |
| 66 | 2× | Board::Existing(n) => { |
| 67 | 2× | let p = pdb::get(&app.db, owner, *n).await.map_err(|e| e.to_string())?.ok_or(format!("{owner} has no board {n}."))?; |
| 68 | 1× | if p.closed { |
| 69 | 0× | return Err(format!("Board {n} is closed.")); |
| 70 | 1× | } |
| 71 | 1× | match crate::web::projects::rights_of(app, Some(account), owner).await.map_err(|e| e.to_string())? { |
| 72 | 1× | Some(r) if r.edit => Ok(()), |
| 73 | 0× | _ => Err(format!("You cannot change {owner}'s boards.")), |
| 74 | | } |
| 75 | | } |
| 76 | | } |
| 77 | 47× | } |
| 78 | |
|
| 79 | |
|
| 80 | 31× | pub async fn link_board(app: &Shared, row: &db::repos::RepoRow, board: &Board, public: bool) -> sqlx::Result<Option<i64>> { |
| 81 | 31× | let p = match board { |
| 82 | 28× | Board::None => return Ok(None), |
| 83 | 2× | Board::New(title) => pdb::create(&app.db, &row.owner, title, &format!("Work on {}/{}", row.owner, row.name), public).await?, |
| 84 | 1× | Board::Existing(n) => match pdb::get(&app.db, &row.owner, *n).await? { |
| 85 | 1× | Some(p) => p, |
| 86 | 0× | None => return Ok(None), |
| 87 | | }, |
| 88 | | }; |
| 89 | 3× | pdb::link_repo(&app.db, p.id, row.id).await?; |
| 90 | 3× | Ok(Some(p.number)) |
| 91 | 31× | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 97 | | pub enum BookmarkScope { |
| 98 | | Site, |
| 99 | | Account(i64), |
| 100 | | Org(i64), |
| 101 | | } |
| 102 | |
|
| 103 | |
|
| 104 | | pub const MAIN: &str = "main"; |
| 105 | |
|
| 106 | | impl BookmarkScope { |
| 107 | 401× | fn key(self) -> String { |
| 108 | 401× | match self { |
| 109 | 248× | BookmarkScope::Site => "default_bookmark".into(), |
| 110 | 107× | BookmarkScope::Account(id) => format!("default_bookmark.account.{id}"), |
| 111 | 46× | BookmarkScope::Org(id) => format!("default_bookmark.org.{id}"), |
| 112 | | } |
| 113 | 401× | } |
| 114 | | } |
| 115 | |
|
| 116 | |
|
| 117 | 396× | pub async fn bookmark_setting(pool: &db::Pool, scope: BookmarkScope) -> sqlx::Result<Option<String>> { |
| 118 | 396× | Ok(db::accounts::setting(pool, &scope.key()).await?.filter(|v| !v.is_empty())) |
| 119 | 396× | } |
| 120 | |
|
| 121 | |
|
| 122 | 2× | pub async fn set_bookmark_setting(pool: &db::Pool, scope: BookmarkScope, value: &str) -> Result<(), String> { |
| 123 | 2× | let v = value.trim(); |
| 124 | 2× | if !v.is_empty() { |
| 125 | 2× | check_bookmark(v)?; |
| 126 | 0× | } |
| 127 | 2× | db::accounts::set_setting(pool, &scope.key(), v).await.map_err(|e| e.to_string()) |
| 128 | 2× | } |
| 129 | |
|
| 130 | |
|
| 131 | 8× | pub fn check_bookmark(name: &str) -> Result<(), String> { |
| 132 | 8× | if crate::repos::names::valid_segment(name) { |
| 133 | 5× | Ok(()) |
| 134 | | } else { |
| 135 | 3× | Err(format!( |
| 136 | 3× | "{name:?} is not a bookmark name: letters, digits, dots, dashes and underscores, up to 64, starting with a letter or digit." |
| 137 | 3× | )) |
| 138 | | } |
| 139 | 8× | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | 92× | pub async fn default_bookmark_for(app: &Shared, owner: &str) -> (String, String) { |
| 144 | 92× | let own = match db::accounts::org_by_name(&app.db, owner).await.ok().flatten() { |
| 145 | 14× | Some(org) => Some(BookmarkScope::Org(org.id)), |
| 146 | 78× | None => db::accounts::account_by_name(&app.db, owner).await.ok().flatten().map(|a| BookmarkScope::Account(a.id)), |
| 147 | | }; |
| 148 | 92× | if let Some(scope) = own { |
| 149 | 92× | if let Ok(Some(b)) = bookmark_setting(&app.db, scope).await { |
| 150 | 2× | return (b, format!("{owner}'s default")); |
| 151 | 90× | } |
| 152 | 0× | } |
| 153 | 90× | match bookmark_setting(&app.db, BookmarkScope::Site).await { |
| 154 | 1× | Ok(Some(b)) => (b, "this site's default".into()), |
| 155 | 89× | _ => (MAIN.into(), "the usual default".into()), |
| 156 | | } |
| 157 | 92× | } |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | 31× | pub async fn set_default_bookmark(app: &Shared, row: db::repos::RepoRow, asked: &str) -> sqlx::Result<db::repos::RepoRow> { |
| 162 | 31× | let asked = asked.trim(); |
| 163 | 31× | let b = if asked.is_empty() { default_bookmark_for(app, &row.owner).await.0 } else { asked.to_string() }; |
| 164 | 31× | if b == row.default_bookmark { |
| 165 | 28× | return Ok(row); |
| 166 | 3× | } |
| 167 | 3× | db::repos::set_default_bookmark(&app.db, row.id, &b).await?; |
| 168 | 3× | Ok(db::repos::get_by_id(&app.db, row.id).await?.unwrap_or(row)) |
| 169 | 31× | } |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 175 | | pub enum Flow { |
| 176 | | Default, |
| 177 | | On, |
| 178 | | Own, |
| 179 | | } |
| 180 | |
|
| 181 | | impl Flow { |
| 182 | 50× | pub fn parse(choice: &str) -> Result<Flow, String> { |
| 183 | 50× | match choice.trim() { |
| 184 | 50× | "" | "default" | "off" => Ok(Flow::Default), |
| 185 | 7× | "on" => Ok(Flow::On), |
| 186 | 4× | "own" => Ok(Flow::Own), |
| 187 | 2× | other => Err(format!("OxygenFlow is on, own or left to the default, not {other}.")), |
| 188 | | } |
| 189 | 50× | } |
| 190 | |
|
| 191 | | |
| 192 | 4× | pub fn setting(self) -> &'static str { |
| 193 | 4× | match self { |
| 194 | 0× | Flow::Default => "", |
| 195 | 3× | Flow::On => "on", |
| 196 | 1× | Flow::Own => "own", |
| 197 | | } |
| 198 | 4× | } |
| 199 | | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | 63× | pub async fn owner_has_runner(app: &Shared, owner: &str) -> sqlx::Result<bool> { |
| 204 | 63× | Ok(db::runners::serving_owner(&app.db, owner).await?.iter().any(|r| !r.scope().is_site())) |
| 205 | 63× | } |
| 206 | |
|
| 207 | |
|
| 208 | 44× | pub async fn check_flow(app: &Shared, account: &db::accounts::Account, owner: &str, flow: Flow) -> Result<(), String> { |
| 209 | 2× | match flow { |
| 210 | 41× | Flow::Default => Ok(()), |
| 211 | 1× | Flow::On if account.is_admin => Ok(()), |
| 212 | 1× | Flow::On => Err("Only site administrators let the site's runners take a repository's jobs.".into()), |
| 213 | 1× | Flow::Own => match owner_has_runner(app, owner).await.map_err(|e| e.to_string())? { |
| 214 | 0× | true => Ok(()), |
| 215 | | false => { |
| 216 | 1× | Err(format!("{owner} has no runners of its own yet: register one, then switch OxygenFlow on in Settings › Pipelines.")) |
| 217 | | } |
| 218 | | }, |
| 219 | | } |
| 220 | 44× | } |
| 221 | |
|
| 222 | |
|
| 223 | 31× | pub async fn set_flow(app: &Shared, row: &db::repos::RepoRow, by: &db::accounts::Account, flow: Flow) -> sqlx::Result<()> { |
| 224 | 31× | if flow == Flow::Default { |
| 225 | 30× | return Ok(()); |
| 226 | 1× | } |
| 227 | 1× | db::repos::set_flow(&app.db, row.id, flow.setting()).await?; |
| 228 | 1× | db::accounts::audit(&app.db, Some(by), "repo.flow", &format!("{}/{}", row.owner, row.name), flow.setting()).await.ok(); |
| 229 | 1× | Ok(()) |
| 230 | 31× | } |
| 231 | |
|
| 232 | |
|
| 233 | 2× | pub fn wiki_home(name: &RepoName, description: &str) -> String { |
| 234 | 2× | let about = description.trim(); |
| 235 | 2× | let about = if about.is_empty() { String::new() } else { format!("{about}\n\n") }; |
| 236 | 2× | format!("# {}\n\n{about}This is the wiki's home page. Edit it, or add pages beside it.\n", name.name) |
| 237 | 2× | } |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | 0× | pub async fn start_wiki(app: &Shared, row: &db::repos::RepoRow, by: &db::accounts::Account, description: &str) -> Result<(), String> { |
| 242 | 0× | let name = RepoName::new(&row.owner, &row.name).ok_or("bad repository name")?; |
| 243 | 0× | let signer = crate::web::edit::signer_for(app, by.id).await?; |
| 244 | 0× | let edit = FileEdit { path: crate::wiki::path_of(crate::wiki::HOME), content: Some(wiki_home(&name, description).into_bytes()) }; |
| 245 | 0× | crate::wiki::commit(app, &name, vec![edit], "Start the wiki".into(), signer).await.map_err(|e| e.to_string())?; |
| 246 | 0× | db::accounts::audit(&app.db, Some(by), "wiki.started", &name.to_string(), "Home").await.ok(); |
| 247 | 0× | Ok(()) |
| 248 | 0× | } |
| 249 | |
|
| 250 | |
|
| 251 | | pub const LICENCES: &[(&str, &str)] = &[ |
| 252 | | ("MIT", "use, change and share it freely, keeping the notice"), |
| 253 | | ("BSD-2-Clause", "as MIT, in the BSD's words"), |
| 254 | | ("ISC", "as MIT, in fewer words"), |
| 255 | | ]; |
| 256 | |
|
| 257 | |
|
| 258 | | pub const IGNORES: &[&str] = &["Rust", "Node", "Python", "Go", "Java and Kotlin"]; |
| 259 | |
|
| 260 | |
|
| 261 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 262 | | pub struct Start { |
| 263 | | pub readme: bool, |
| 264 | | |
| 265 | | pub licence: String, |
| 266 | | |
| 267 | | pub ignore: String, |
| 268 | | } |
| 269 | |
|
| 270 | | impl Start { |
| 271 | 1× | pub fn is_empty(&self) -> bool { |
| 272 | 1× | !self.readme && self.licence.is_empty() && self.ignore.is_empty() |
| 273 | 1× | } |
| 274 | |
|
| 275 | | |
| 276 | | |
| 277 | 44× | pub fn files(&self, name: &RepoName, description: &str, holder: &str, year: i32) -> Result<Vec<(String, String)>, String> { |
| 278 | 44× | let mut out = Vec::new(); |
| 279 | 44× | if self.readme { |
| 280 | 3× | let about = if description.trim().is_empty() { String::new() } else { format!("{}\n\n", description.trim()) }; |
| 281 | 3× | out.push(("README.md".to_string(), format!("# {}\n\n{about}", name.name))); |
| 282 | 41× | } |
| 283 | 44× | if !self.licence.is_empty() { |
| 284 | 2× | let text = licence(&self.licence, holder, year).ok_or(format!("No licence {}.", self.licence))?; |
| 285 | 1× | out.push(("LICENSE".to_string(), text)); |
| 286 | 42× | } |
| 287 | 43× | if !self.ignore.is_empty() { |
| 288 | 1× | let text = ignore(&self.ignore).ok_or(format!("No .oxyignore for {}.", self.ignore))?; |
| 289 | 1× | out.push((".oxyignore".to_string(), text.to_string())); |
| 290 | 42× | } |
| 291 | 43× | Ok(out) |
| 292 | 44× | } |
| 293 | | } |
| 294 | |
|
| 295 | 5× | fn licence(id: &str, holder: &str, year: i32) -> Option<String> { |
| 296 | 5× | Some(match id { |
| 297 | 5× | "MIT" => format!( |
| 298 | | "MIT License\n\nCopyright (c) {year} {holder}\n\n\ |
| 299 | | Permission is hereby granted, free of charge, to any person obtaining a copy\n\ |
| 300 | | of this software and associated documentation files (the \"Software\"), to deal\n\ |
| 301 | | in the Software without restriction, including without limitation the rights\n\ |
| 302 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n\ |
| 303 | | copies of the Software, and to permit persons to whom the Software is\n\ |
| 304 | | furnished to do so, subject to the following conditions:\n\n\ |
| 305 | | The above copyright notice and this permission notice shall be included in all\n\ |
| 306 | | copies or substantial portions of the Software.\n\n\ |
| 307 | | THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\ |
| 308 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\ |
| 309 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\ |
| 310 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\ |
| 311 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\ |
| 312 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\ |
| 313 | | SOFTWARE.\n" |
| 314 | | ), |
| 315 | 3× | "BSD-2-Clause" => format!( |
| 316 | | "BSD 2-Clause License\n\nCopyright (c) {year}, {holder}\n\n\ |
| 317 | | Redistribution and use in source and binary forms, with or without\n\ |
| 318 | | modification, are permitted provided that the following conditions are met:\n\n\ |
| 319 | | 1. Redistributions of source code must retain the above copyright notice, this\n \ |
| 320 | | list of conditions and the following disclaimer.\n\n\ |
| 321 | | 2. Redistributions in binary form must reproduce the above copyright notice,\n \ |
| 322 | | this list of conditions and the following disclaimer in the documentation\n \ |
| 323 | | and/or other materials provided with the distribution.\n\n\ |
| 324 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n\ |
| 325 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n\ |
| 326 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n\ |
| 327 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n\ |
| 328 | | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n\ |
| 329 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n\ |
| 330 | | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n\ |
| 331 | | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n\ |
| 332 | | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n\ |
| 333 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" |
| 334 | | ), |
| 335 | 2× | "ISC" => format!( |
| 336 | | "ISC License\n\nCopyright (c) {year} {holder}\n\n\ |
| 337 | | Permission to use, copy, modify, and/or distribute this software for any\n\ |
| 338 | | purpose with or without fee is hereby granted, provided that the above\n\ |
| 339 | | copyright notice and this permission notice appear in all copies.\n\n\ |
| 340 | | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n\ |
| 341 | | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n\ |
| 342 | | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n\ |
| 343 | | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n\ |
| 344 | | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n\ |
| 345 | | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n\ |
| 346 | | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n" |
| 347 | | ), |
| 348 | 1× | _ => return None, |
| 349 | | }) |
| 350 | 5× | } |
| 351 | |
|
| 352 | 6× | fn ignore(lang: &str) -> Option<&'static str> { |
| 353 | 6× | Some(match lang { |
| 354 | 6× | "Rust" => "# Build output\ntarget/\n", |
| 355 | 4× | "Node" => "# Dependencies and build output\nnode_modules/\ndist/\n*.log\n", |
| 356 | 3× | "Python" => "# Caches, environments and build output\n__pycache__/\n*.pyc\n.venv/\nbuild/\ndist/\n*.egg-info/\n", |
| 357 | 2× | "Go" => "# Build output\nbin/\n*.test\n", |
| 358 | 1× | "Java and Kotlin" => "# Build output and tool state\nbuild/\n.gradle/\nout/\n*.class\n", |
| 359 | 0× | _ => return None, |
| 360 | | }) |
| 361 | 6× | } |
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | 31× | pub async fn commit_start( |
| 367 | 31× | app: &Shared, |
| 368 | 31× | row: &db::repos::RepoRow, |
| 369 | 31× | by: &db::accounts::Account, |
| 370 | 31× | files: Vec<(String, String)>, |
| 371 | 31× | ) -> Result<(), String> { |
| 372 | 31× | if files.is_empty() { |
| 373 | 31× | return Ok(()); |
| 374 | 0× | } |
| 375 | 0× | let name = RepoName::new(&row.owner, &row.name).ok_or("bad repository name")?; |
| 376 | 0× | let signer = crate::web::edit::signer_for(app, by.id).await?; |
| 377 | 0× | let paths: Vec<String> = files.iter().map(|(p, _)| p.clone()).collect(); |
| 378 | 0× | let edits: Vec<FileEdit> = files.into_iter().map(|(path, content)| FileEdit { path, content: Some(content.into_bytes()) }).collect(); |
| 379 | 0× | let b = row.default_bookmark.clone(); |
| 380 | 0× | crate::repos::edits::commit(&app.repos, &name, b.clone(), b, edits, format!("Start with {}", paths.join(", ")), signer) |
| 381 | 0× | .await |
| 382 | 0× | .map_err(|e| e.to_string())?; |
| 383 | 0× | db::accounts::audit(&app.db, Some(by), "repo.started", &name.to_string(), &paths.join(", ")).await.ok(); |
| 384 | 0× | Ok(()) |
| 385 | 31× | } |
| 386 | |
|
| 387 | | #[cfg(test)] |
| 388 | | mod tests { |
| 389 | | use super::*; |
| 390 | |
|
| 391 | | #[test] |
| 392 | 1× | fn boards_and_starting_files() { |
| 393 | 1× | assert_eq!(Board::parse("", "", "demo").unwrap(), Board::None); |
| 394 | 1× | assert_eq!(Board::parse("new", " ", "demo").unwrap(), Board::New("demo".into())); |
| 395 | 1× | assert_eq!(Board::parse("new", "Roadmap", "demo").unwrap(), Board::New("Roadmap".into())); |
| 396 | 1× | assert_eq!(Board::parse("4", "", "demo").unwrap(), Board::Existing(4)); |
| 397 | 1× | assert!(Board::parse("four", "", "demo").is_err()); |
| 398 | 1× | let name = RepoName::new("andy", "demo").unwrap(); |
| 399 | 1× | let s = Start { readme: true, licence: "MIT".into(), ignore: "Rust".into() }; |
| 400 | 1× | let files = s.files(&name, "A demo.", "Andy McCall", 2026).unwrap(); |
| 401 | 3× | let paths: Vec<&str> = files.iter().map(|(p, _)| p.as_str()).collect(); |
| 402 | 1× | assert_eq!(paths, ["README.md", "LICENSE", ".oxyignore"]); |
| 403 | 1× | assert_eq!(files[0].1, "# demo\n\nA demo.\n\n"); |
| 404 | 1× | assert!(files[1].1.starts_with("MIT License\n\nCopyright (c) 2026 Andy McCall\n")); |
| 405 | 1× | assert_eq!(files[2].1, "# Build output\ntarget/\n"); |
| 406 | 3× | for (id, _) in LICENCES { |
| 407 | 3× | assert!(licence(id, "X", 2026).unwrap().contains("2026"), "{id}"); |
| 408 | | } |
| 409 | 5× | for lang in IGNORES { |
| 410 | 5× | assert!(ignore(lang).is_some(), "{lang}"); |
| 411 | | } |
| 412 | 1× | assert!(Start { licence: "GPL".into(), ..Default::default() }.files(&name, "", "X", 2026).is_err()); |
| 413 | 1× | assert!(Start::default().is_empty()); |
| 414 | 1× | } |
| 415 | |
|
| 416 | | #[test] |
| 417 | 1× | fn flow_choices_and_the_wiki_home() { |
| 418 | 1× | assert_eq!(Flow::parse("").unwrap(), Flow::Default); |
| 419 | 1× | assert_eq!(Flow::parse("off").unwrap(), Flow::Default); |
| 420 | 1× | assert_eq!(Flow::parse("on").unwrap().setting(), "on"); |
| 421 | 1× | assert_eq!(Flow::parse("own").unwrap().setting(), "own"); |
| 422 | 1× | assert!(Flow::parse("always").is_err()); |
| 423 | 1× | let name = RepoName::new("andy", "square").unwrap(); |
| 424 | 1× | assert_eq!( |
| 425 | 1× | wiki_home(&name, " A platform game. "), |
| 426 | | "# square\n\nA platform game.\n\nThis is the wiki's home page. Edit it, or add pages beside it.\n" |
| 427 | | ); |
| 428 | 1× | assert!(wiki_home(&name, "").starts_with("# square\n\nThis is")); |
| 429 | 1× | } |
| 430 | |
|
| 431 | | #[test] |
| 432 | 1× | fn bookmark_defaults_are_kept_apart() { |
| 433 | 1× | assert_eq!(BookmarkScope::Site.key(), "default_bookmark"); |
| 434 | 1× | assert_eq!(BookmarkScope::Account(7).key(), "default_bookmark.account.7"); |
| 435 | 1× | assert_eq!(BookmarkScope::Org(7).key(), "default_bookmark.org.7"); |
| 436 | 1× | assert!(check_bookmark("develop").is_ok()); |
| 437 | 1× | assert!(check_bookmark("release-1.0").is_ok()); |
| 438 | 1× | assert!(check_bookmark("feature/x").is_err()); |
| 439 | 1× | assert!(check_bookmark("-x").is_err()); |
| 440 | 1× | } |
| 441 | | } |