radicle-git init

Port over all git relevant parts of the radicle-link repository[0].
The packages that are kept are:
* git-ext
* git-ref-format
* git-trailers
* link-git
* macros
* std-ext
* test -- only the portions that test the above

The development and testing scripts are also added for local and CI
testing.

[0]: https://github.com/radicle-dev/radicle-link/
Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
This commit is contained in:
Fintan Halpenny
2022-09-08 12:07:51 +01:00
parent c43420956c
commit 70b571b67b
112 changed files with 10341 additions and 7 deletions
+4
View File
@@ -0,0 +1,4 @@
// Copyright © 2022 The Radicle Link Contributors
// SPDX-License-Identifier: GPLv3-or-later
pub mod std_net;
+47
View File
@@ -0,0 +1,47 @@
// Copyright © 2022 The Radicle Link Contributors
// SPDX-License-Identifier: GPLv3-or-later
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use proptest::prelude::*;
prop_compose! {
pub fn gen_ipaddr_v4()
(a in any::<u8>(),
b in any::<u8>(),
c in any::<u8>(),
d in any::<u8>()) -> Ipv4Addr{
Ipv4Addr::new(a, b, c, d)
}
}
prop_compose! {
pub fn gen_ipaddr_v6()
(a in any::<u16>(),
b in any::<u16>(),
c in any::<u16>(),
d in any::<u16>(),
e in any::<u16>(),
f in any::<u16>(),
g in any::<u16>(),
h in any::<u16>()) -> Ipv6Addr
{
Ipv6Addr::new(a, b, c, d, e, f, g, h)
}
}
pub fn gen_socket_v4() -> impl Strategy<Value = SocketAddr> {
any::<u16>().prop_flat_map(move |port| {
gen_ipaddr_v4().prop_map(move |v4| SocketAddr::V4(SocketAddrV4::new(v4, port)))
})
}
pub fn gen_socket_v6() -> impl Strategy<Value = SocketAddr> {
any::<u16>().prop_flat_map(move |port| {
gen_ipaddr_v6().prop_map(move |v6| SocketAddr::V6(SocketAddrV6::new(v6, port, 0, 0)))
})
}
pub fn gen_socket_addr() -> impl Strategy<Value = SocketAddr> {
prop_oneof![gen_socket_v4(), gen_socket_v6()]
}
+7
View File
@@ -0,0 +1,7 @@
// Copyright © 2022 The Radicle Link Contributors
// SPDX-License-Identifier: GPLv3-or-later
pub mod gen;
pub mod logging;
pub mod roundtrip;
pub mod tempdir;
+56
View File
@@ -0,0 +1,56 @@
// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.
use std::env;
use log::{log_enabled, Level};
use tracing::subscriber::set_global_default as set_subscriber;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
/// Initialise logging / tracing
///
/// Note that this will capture logs, so they can be output as part of the test
/// output. Use `RUST_LOG` with care, as this may create unwanted memory
/// pressure. Note, however, that if `RUST_LOG` is not set, we set the level to
/// `error` by default in order to surface errors on CI.
///
/// The `TRACING_FMT` environment variable can be used to control the log
/// formatting. Supported values:
///
/// * "pretty": [`tracing_subscriber::fmt::format::Pretty`]
/// * "compact": [`tracing_subscriber::fmt::format::Compact`]
/// * "json": [`tracing_subscriber::fmt::format::Json`]
///
/// If the variable is not set, or set to any other value, the
/// [`tracing_subscriber::fmt::format::Full`] format is used.
pub fn init() {
if env_logger::builder().is_test(true).try_init().is_ok() {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "debug");
}
let mut builder = FmtSubscriber::builder()
.with_env_filter(EnvFilter::from_default_env())
.with_test_writer();
if log_enabled!(target: "librad", Level::Trace) {
builder = builder.with_thread_ids(true);
} else if env::var("TRACING_FMT").is_err() {
let default_format = if env::var("CI").is_ok() {
"compact"
} else {
"pretty"
};
env::set_var("TRACING_FMT", default_format);
}
match env::var("TRACING_FMT").ok().as_deref() {
Some("pretty") => set_subscriber(builder.pretty().finish()),
Some("compact") => set_subscriber(builder.compact().finish()),
Some("json") => set_subscriber(builder.json().flatten_event(true).finish()),
_ => set_subscriber(builder.finish()),
}
.expect("setting tracing subscriber failed")
}
}
+36
View File
@@ -0,0 +1,36 @@
// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.
use std::{
fmt::{Debug, Display},
str::FromStr,
};
use pretty_assertions::assert_eq;
pub fn json<A>(a: A)
where
for<'de> A: Debug + PartialEq + serde::Serialize + serde::Deserialize<'de>,
{
assert_eq!(
a,
serde_json::from_str(&serde_json::to_string(&a).unwrap()).unwrap()
)
}
pub fn cbor<A>(a: A)
where
for<'de> A: Debug + PartialEq + minicbor::Encode + minicbor::Decode<'de>,
{
assert_eq!(a, minicbor::decode(&minicbor::to_vec(&a).unwrap()).unwrap())
}
pub fn str<A>(a: A)
where
A: Debug + PartialEq + Display + FromStr,
<A as FromStr>::Err: Debug,
{
assert_eq!(a, a.to_string().parse().unwrap())
}
+43
View File
@@ -0,0 +1,43 @@
// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.
use std::{
io,
ops::{Deref, DerefMut},
path::Path,
};
use tempfile::{tempdir, TempDir};
pub struct WithTmpDir<A> {
_tmp: TempDir,
inner: A,
}
impl<A> WithTmpDir<A> {
pub fn new<F, E>(mk_inner: F) -> Result<Self, E>
where
F: FnOnce(&Path) -> Result<A, E>,
E: From<io::Error>,
{
let tmp = tempdir()?;
let inner = mk_inner(tmp.path())?;
Ok(Self { _tmp: tmp, inner })
}
}
impl<A> Deref for WithTmpDir<A> {
type Target = A;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<A> DerefMut for WithTmpDir<A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}