From d331f0b7ae4657914c0ec1be9051f7047f7a3810 Mon Sep 17 00:00:00 2001
From: Louis <836250617@qq.com>
Date: Sun, 17 Dec 2023 18:10:38 +0800
Subject: [PATCH] test
---
.github/workflows/release.yml | 41 +++++++------
.github/workflows/test.yml | 33 +++++-----
README.md | 2 +-
tests/tests.rs => examples/example.rs | 7 ++-
src/config.rs | 15 +++--
src/lib.rs | 87 ++++++++++++++++++++++++++-
tests/.gitkeep | 1 +
7 files changed, 140 insertions(+), 46 deletions(-)
rename tests/tests.rs => examples/example.rs (97%)
create mode 100644 tests/.gitkeep
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index b86ecc4..9297e8b 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -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/setup-rust-toolchain@v1.6.0
-
- - 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}}
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 79c46b6..b8d353e 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -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/setup-rust-toolchain@v1.6.0
-
- - 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
diff --git a/README.md b/README.md
index b368f20..2fffa25 100644
--- a/README.md
+++ b/README.md
@@ -87,7 +87,7 @@ A rust crate to manage, persist, encrypt configurations.
## Getting Started
-Details here: [Example](tests/tests.rs)
+Details here: [Example](example/eamples.rs)
(back to top)
diff --git a/tests/tests.rs b/examples/example.rs
similarity index 97%
rename from tests/tests.rs
rename to examples/example.rs
index 63fc0c5..29d7507 100644
--- a/tests/tests.rs
+++ b/examples/example.rs
@@ -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();
@@ -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();
+}
diff --git a/src/config.rs b/src/config.rs
index 7bc9602..02387ec 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -10,7 +10,7 @@ pub(crate) type ConfigValue = Vec;
/// 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");
@@ -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");
@@ -144,11 +144,13 @@ impl ConfigPatch {
}
}
+type Func = Box ConfigResult>;
+
/// 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");
@@ -184,14 +186,11 @@ impl ConfigPatch {
/// ```
pub struct SecretConfigPatch {
key: ConfigKey,
- func: Box ConfigResult>,
+ func: Func,
}
impl SecretConfigPatch {
- pub(crate) fn new(
- key: ConfigKey,
- func: Box ConfigResult>>,
- ) -> Self {
+ pub(crate) fn new(key: ConfigKey, func: Func) -> Self {
Self { key, func }
}
diff --git a/src/lib.rs b/src/lib.rs
index 8bebe28..681587f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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 {
+ 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();
+ }
+}
diff --git a/tests/.gitkeep b/tests/.gitkeep
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/tests/.gitkeep
@@ -0,0 +1 @@
+