WIP: mvp flake working

This still has an issue, because tests cannot run with network inside of
the flake.

When this is solved, the code just needs to be refactored and it's
ready.

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-06-17 14:11:42 +02:00
parent ce2ceee129
commit 551c406591
Signed by: cafkafk
GPG key ID: CDDC792F655251ED
12 changed files with 186 additions and 29 deletions

7
Cargo.lock generated
View file

@ -196,6 +196,7 @@ dependencies = [
"clap_mangen",
"log",
"pretty_env_logger",
"relative-path",
"serde",
"serde_yaml",
]
@ -359,6 +360,12 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
[[package]]
name = "relative-path"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf2521270932c3c7bed1a59151222bd7643c79310f2916f01925e1e16255698"
[[package]]
name = "roff"
version = "0.2.1"

View file

@ -16,6 +16,7 @@ clap = { version = "4.0.22", features = ["derive"] }
log = "0.4"
#env_logger = "0.9"
pretty_env_logger = "0.4"
relative-path = "1.8.0"
[build-dependencies]
clap = { version = "4.3.2", features = ["derive", "cargo", "env", "help"] }

View file

@ -134,7 +134,8 @@
"crane": "crane",
"fenix": "fenix",
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs",
"test-directory": "test-directory"
}
},
"rust-overlay": {
@ -191,6 +192,18 @@
"repo": "default",
"type": "github"
}
},
"test-directory": {
"flake": false,
"locked": {
"narHash": "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=",
"type": "file",
"url": "file:///home/ces/org/src/git/gg"
},
"original": {
"type": "file",
"url": "file:///home/ces/org/src/git/gg"
}
}
},
"root": "root",

View file

@ -21,9 +21,14 @@
url = "github:rustsec/advisory-db";
flake = false;
};
test-directory = {
url = "file:///home/ces/org/src/git/gg";
flake = false;
};
};
outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }:
outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, test-directory, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
@ -33,13 +38,35 @@
inherit (pkgs) lib;
craneLib = crane.lib.${system};
src = craneLib.cleanCargoSource (craneLib.path ./.);
#src = craneLib.cleanCargoSource (craneLib.path ./.);
# When filtering sources, we want to allow assets other than .rs files
src = lib.cleanSourceWith {
src = ./.; # The original, unfiltered source
filter = path: type:
(lib.hasSuffix "\.yaml" path) ||
# (lib.hasSuffix "\.scss" path) ||
# Example of a folder for images, icons, etc
#(lib.hasInfix "test" path) ||
# Default filter from crane (allow .rs files)
(craneLib.filterCargoSources path type)
;
};
# src = pkgs.lib.cleanSourceWith {
# src = craneLib.path ./.; # original, unfiltered source
# filter = path: type:
# (builtins.match ".*yaml" path != null) # include JSONC files
# || (craneLib.filterCargoSources path type);
# };
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit src;
buildInputs = [
# test-directory
# Add additional build inputs here
] ++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
@ -48,6 +75,7 @@
# Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value";
RUST_BACKTRACE = 1;
};
craneLibLLvmTools = craneLib.overrideToolchain
@ -65,6 +93,7 @@
# artifacts from above.
my-crate = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
# doCheck = false; # NOTE remove me
});
in
{
@ -100,6 +129,7 @@
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on `my-crate` if you do not want
# the tests to run twice
# NOTE enable this part
my-crate-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
partitions = 1;
@ -108,9 +138,9 @@
} // lib.optionalAttrs (system == "x86_64-linux") {
# NB: cargo-tarpaulin only supports x86_64 systems
# Check code coverage (note: this will not upload coverage anywhere)
my-crate-coverage = craneLib.cargoTarpaulin (commonArgs // {
inherit cargoArtifacts;
});
# my-crate-coverage = craneLib.cargoTarpaulin (commonArgs // {
# inherit cargoArtifacts;
# });
};
packages = {

View file

@ -99,7 +99,7 @@ impl GitRepo {
.arg(&self.url)
.arg(&self.name)
.status()
.expect("failed to add");
.unwrap_or_else(|_| panic!("git repo failed to add: {:?}", &self,));
info!("{out}");
} else {
info!("{} has clone set to false, not cloned", &self.name);
@ -111,7 +111,7 @@ impl GitRepo {
.current_dir(format!("{}{}", &self.path, &self.name))
.arg("pull")
.status()
.expect("failed to pull");
.unwrap_or_else(|_| panic!("git repo failed to pull: {:?}", &self,));
info!("{out}");
}
/// Adds all files in the repository.
@ -167,9 +167,16 @@ impl Config {
/// Reads the configuration toml from a path.
pub fn new(path: &String) -> Self {
debug!("initializing new Config struct");
let yaml = fs::read_to_string(path).expect("Should have been able to read the file");
let yaml = fs::read_to_string(path).unwrap_or_else(|_| {
panic!("Should have been able to read the file: path -> {:?}", path,)
});
debug!("deserialized yaml from config file");
serde_yaml::from_str(&yaml).expect("Should have been able to deserialize yaml config")
serde_yaml::from_str(&yaml).unwrap_or_else(|_| {
panic!(
"Should have been able to deserialize yaml config: path -> {:?}",
path,
)
})
}
/// Tries to pull all repositories, skips if fail.
pub fn pull_all(&self) {

View file

@ -72,9 +72,11 @@ fn main() {
mod config {
use crate::*;
use git::GitRepo;
use relative_path::RelativePath;
use std::env::current_dir;
use std::fs::File;
use std::io::prelude::*;
use utils::dir::current_dir;
//use utils::dir::current_dir;
#[test]
fn init_config() {
let _config = Config {
@ -102,30 +104,43 @@ mod config {
}
#[test]
fn read_config_populate() {
const CONFIG_FILE: &str = "/tst/config.yaml";
let config_path = current_dir() + CONFIG_FILE;
let _config = Config::new(&config_path);
let _config = Config::new(&RelativePath::new("./src/test/config.yaml").to_string());
}
#[test]
fn write_config() {
const CONFIG_FILE: &str = "/tst/config.yaml";
const CONFIG_TEST: &str = "/tst/test.yaml";
let config_path = current_dir() + CONFIG_FILE;
let config = Config::new(&config_path);
let root = current_dir().unwrap();
let config = Config::new(
&RelativePath::new("./src/test/config.yaml")
.to_logical_path(&root)
.into_os_string()
.into_string()
.unwrap(),
);
let test_path = current_dir() + CONFIG_TEST;
let mut file = File::create(&test_path).unwrap();
let mut test_file = File::create(
RelativePath::new("./src/test/test.yaml")
.to_logical_path(&root)
.into_os_string()
.into_string()
.unwrap(),
)
.expect("failed to create test file");
let contents = serde_yaml::to_string(&config).unwrap();
file.write_all(contents.as_bytes()).unwrap();
test_file.write_all(contents.as_bytes()).unwrap();
let test_config = Config::new(&test_path);
let test_config = Config::new(&RelativePath::new("./src/test/test.yaml").to_string());
assert_eq!(config, test_config);
}
#[test]
fn read_and_verify_config() {
const CONFIG_FILE: &str = "/tst/config.yaml";
let config_path = current_dir() + CONFIG_FILE;
let config = Config::new(&config_path);
let root = current_dir().unwrap();
let config = Config::new(
&RelativePath::new("./src/test/config.yaml")
.to_logical_path(root)
.into_os_string()
.into_string()
.unwrap(),
);
// FIXME This is unnecessarily terse
#[allow(clippy::bool_assert_comparison)]
{
@ -163,17 +178,24 @@ mod config {
}
}
/* Unable to test with networking inside flake
#[cfg(test)]
mod repo_actions {
use crate::*;
use git::GitRepo;
use relative_path::RelativePath;
use std::env::current_dir;
use std::process::Command;
use utils::dir::current_dir;
#[test]
#[allow(clippy::redundant_clone)]
fn test_repo_actions() {
let test_repo_name: String = "test".to_string();
let test_repo_dir: String = (current_dir() + "/tst/").to_string();
let root = current_dir().unwrap();
let test_repo_dir: String = RelativePath::new("./src/test")
.to_logical_path(&root)
.into_os_string()
.into_string()
.unwrap();
let test_repo_url: String = "git@github.com:cafkafk/test.git".to_string();
println!("{}", test_repo_dir);
let mut config = Config {
@ -187,8 +209,10 @@ mod repo_actions {
clone: true,
};
config.repos.push(repo);
config.clone_all();
config.pull_all();
// BUG FIXME can't do this in flake
// should have a good alternative
// config.clone_all();
// config.pull_all();
for r in config.repos.iter() {
Command::new("touch")
.current_dir(&(r.path.to_owned() + &r.name))
@ -200,3 +224,4 @@ mod repo_actions {
config.commit_all_msg(&"test".to_string());
}
}
*/

1
src/test/test Submodule

@ -0,0 +1 @@
Subproject commit 318edb997ddb88b3dd255b70c8b73e1ced7b74f9

24
src/test/test.yaml Normal file
View file

@ -0,0 +1,24 @@
repos:
- name: gg
path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git
clone: true
- name: li
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git
clone: true
- name: qmk_firmware
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git
clone: true
- name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
clone: true
links:
- name: gg
rx: /home/ces/.config/gg
tx: /home/ces/.dots/gg
- name: starship
rx: /home/ces/.config/starship.toml
tx: /home/ces/.dots/starship.toml

24
test/config.yaml Normal file
View file

@ -0,0 +1,24 @@
repos:
- name: gg
path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git
clone: true
- name: li
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git
clone: true
- name: qmk_firmware
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git
clone: true
- name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
clone: true
links:
- name: gg
rx: /home/ces/.config/gg
tx: /home/ces/.dots/gg
- name: starship
rx: /home/ces/.config/starship.toml
tx: /home/ces/.dots/starship.toml

1
test/test Submodule

@ -0,0 +1 @@
Subproject commit 318edb997ddb88b3dd255b70c8b73e1ced7b74f9

24
test/test.yaml Normal file
View file

@ -0,0 +1,24 @@
repos:
- name: gg
path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git
clone: true
- name: li
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git
clone: true
- name: qmk_firmware
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git
clone: true
- name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
clone: true
links:
- name: gg
rx: /home/ces/.config/gg
tx: /home/ces/.dots/gg
- name: starship
rx: /home/ces/.config/starship.toml
tx: /home/ces/.dots/starship.toml