Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwingfly committed Dec 17, 2023
1 parent 7baf373 commit d331f0b
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 46 deletions.
41 changes: 22 additions & 19 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ name: Rust

on:
push:
branches: [ "release" ]
branches: ["release"]
pull_request:
branches: [ "release" ]
branches: ["release"]

env:
CARGO_TERM_COLOR: always

jobs:
build:

publish:
runs-on: ubuntu-latest

steps:
- name: Setup Rust Toolchain for GitHub CI
uses: actions-rust-lang/[email protected]

- uses: actions/checkout@v3
- name: Run clippy
run: cargo clippy --all-features --all-targets -- -D warnings
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: clippy

- uses: actions/checkout@v3
- name: Run clippy
run: cargo clippy --all-features --all-targets -- -D warnings
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

- name: publish crates
uses: katyo/publish-crates@v2
with:
# Cargo registry token
registry-token: ${{secrets.crates_io}}
- name: publish crates
uses: katyo/publish-crates@v2
with:
# Cargo registry token
registry-token: ${{secrets.crates_io}}
33 changes: 18 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ name: Rust

on:
push:
branches: [ "dev" ]
branches: ["dev"]
pull_request:
branches: [ "dev" ]
branches: ["dev"]

env:
CARGO_TERM_COLOR: always

jobs:
build:

test:
runs-on: ubuntu-latest

steps:
- name: Setup Rust Toolchain for GitHub CI
uses: actions-rust-lang/[email protected]

- uses: actions/checkout@v3

- name: Run clippy
run: cargo clippy --all-features --all-targets -- -D warnings
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: clippy

- uses: actions/checkout@v3

- name: Run clippy
run: cargo clippy --all-features --all-targets -- -D warnings
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ A rust crate to manage, persist, encrypt configurations.
<!-- GETTING STARTED -->
## Getting Started

Details here: [Example](tests/tests.rs)
Details here: [Example](example/eamples.rs)

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
7 changes: 5 additions & 2 deletions tests/tests.rs → examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl SecretSource for SecretSourceImpl {
}
}

#[test]
fn source_test() {
fn config_tests() {
let mut config = Config::new("test");
config.add_source(NormalSource).unwrap();
config.add_persist_source(PersistSourceImpl).unwrap();
Expand Down Expand Up @@ -79,3 +78,7 @@ fn source_test() {
std::fs::remove_file("tests/secret_test").unwrap();
std::fs::remove_file("tests/test").unwrap();
}

fn main() {
config_tests();
}
15 changes: 7 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) type ConfigValue = Vec<u8>;

/// A struct that can be used to store configuration values.
/// # Example
/// ```
/// ```no_run
/// use encrypt_config::{Config, Source, ConfigResult};
///
/// let mut config = Config::new("test");
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Config {
/// You can get a `ConfigPatch` by calling [`PersistSource::upgrade`], and apply it by calling [`ConfigPatch::apply`] to a config.
/// No change will happen until you call [`ConfigPatch::apply`].
/// # Example
/// ```rust
/// ```no_run
/// use encrypt_config::{Config, ConfigKey, PersistSource, ConfigResult};
///
/// let mut config = Config::new("test");
Expand Down Expand Up @@ -144,11 +144,13 @@ impl ConfigPatch {
}
}

type Func = Box<dyn FnOnce(&Encrypter) -> ConfigResult<ConfigValue>>;

/// A patch that can be used to modify the config.
/// You can get a `SecretConfigPatch` by calling [`SecretSource::upgrade`], and apply it by calling [`SecretConfigPatch::apply`] to a config.
/// No change will happen until you call [`SecretConfigPatch::apply`].
/// # Example
/// ```rust
/// ```no_run
/// use encrypt_config::{Config, ConfigKey, SecretSource, ConfigResult};
///
/// let mut config = Config::new("test");
Expand Down Expand Up @@ -184,14 +186,11 @@ impl ConfigPatch {
/// ```
pub struct SecretConfigPatch {
key: ConfigKey,
func: Box<dyn FnOnce(&Encrypter) -> ConfigResult<ConfigValue>>,
func: Func,
}

impl SecretConfigPatch {
pub(crate) fn new(
key: ConfigKey,
func: Box<dyn FnOnce(&Encrypter) -> ConfigResult<Vec<u8>>>,
) -> Self {
pub(crate) fn new(key: ConfigKey, func: Func) -> Self {
Self { key, func }
}

Expand Down
87 changes: 86 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,95 @@ pub trait SecretSource {
let new_value = serde_json::to_vec(new_value).unwrap();
let func = Box::new(move |encrypter: &Encrypter| {
let encrypted = encrypter.encrypt_serded(&new_value).unwrap();
std::fs::write(path, &encrypted).unwrap();
std::fs::write(path, encrypted).unwrap();
Ok(new_value)
});
let patch = SecretConfigPatch::new(self.source_name(), func);
Ok(patch)
}
}

#[cfg(test)]
mod tests {
use super::*;

struct NormalSource;
impl Source for NormalSource {
type Value = String;
type Map = Vec<(String, Self::Value)>;

fn collect(&self) -> ConfigResult<Self::Map> {
Ok(vec![("key".to_owned(), "value".to_owned())])
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct Foo(String);

struct PersistSourceImpl;
impl PersistSource for PersistSourceImpl {
type Value = Foo;

fn source_name(&self) -> ConfigKey {
"test".to_owned()
}

fn default(&self) -> Self::Value {
Foo("hello".to_owned())
}

fn path(&self) -> std::path::PathBuf {
std::path::PathBuf::from("tests").join(self.source_name())
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct Bar(String);

struct SecretSourceImpl;
impl SecretSource for SecretSourceImpl {
type Value = Bar;

fn source_name(&self) -> ConfigKey {
"secret_test".to_owned()
}

fn default(&self) -> Self::Value {
Bar("world".to_owned())
}

fn path(&self) -> std::path::PathBuf {
std::path::PathBuf::from("tests").join(self.source_name())
}
}

#[test]
fn config_tests() {
let mut config = Config::new("test");
config.add_source(NormalSource).unwrap();
config.add_persist_source(PersistSourceImpl).unwrap();
config.add_secret_source(SecretSourceImpl).unwrap();
let v: String = config.get("key").unwrap();
assert_eq!(v, "value");
let v: Foo = config.get("test").unwrap();
assert_eq!(v, Foo("hello".to_owned()));
let v: Bar = config.get("secret_test").unwrap();
assert_eq!(v, Bar("world".to_owned()));
let patch = NormalSource
.upgrade("key", &"new_value".to_owned())
.unwrap();
patch.apply(&mut config).unwrap();
let v: String = config.get("key").unwrap();
assert_eq!(v, "new_value");
let patch = PersistSourceImpl.upgrade(&Foo("hi".to_owned())).unwrap();
patch.apply(&mut config).unwrap();
let v: Foo = config.get("test").unwrap();
assert_eq!(v, Foo("hi".to_owned()));
let patch = SecretSourceImpl.upgrade(&Bar("Louis".to_owned())).unwrap();
patch.apply(&mut config).unwrap();
let v: Bar = config.get("secret_test").unwrap();
assert_eq!(v, Bar("Louis".to_owned()));
std::fs::remove_file("tests/secret_test").unwrap();
std::fs::remove_file("tests/test").unwrap();
}
}
1 change: 1 addition & 0 deletions tests/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit d331f0b

Please sign in to comment.