diff --git a/input/penumbra-testnet-deimos-6.json b/input/chains/penumbra-testnet-deimos-6.json similarity index 94% rename from input/penumbra-testnet-deimos-6.json rename to input/chains/penumbra-testnet-deimos-6.json index 0b0793e..d67ca87 100644 --- a/input/penumbra-testnet-deimos-6.json +++ b/input/chains/penumbra-testnet-deimos-6.json @@ -1,19 +1,5 @@ { "chainId": "penumbra-testnet-deimos-6", - "rpcs": [ - { - "name": "Penumbra Labs Testnet RPC", - "url": "https://grpc.testnet.penumbra.zone", - "images": [ - { - "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" - } - ] - } - ], - "frontends": [ - "https://app.testnet.penumbra.zone" - ], "ibcConnections": [ { "displayName": "Osmosis", diff --git a/input/penumbra-testnet-deimos-7.json b/input/chains/penumbra-testnet-deimos-7.json similarity index 94% rename from input/penumbra-testnet-deimos-7.json rename to input/chains/penumbra-testnet-deimos-7.json index 174b687..b090fe4 100644 --- a/input/penumbra-testnet-deimos-7.json +++ b/input/chains/penumbra-testnet-deimos-7.json @@ -1,19 +1,5 @@ { "chainId": "penumbra-testnet-deimos-7", - "rpcs": [ - { - "name": "Penumbra Labs Testnet RPC", - "url": "https://grpc.testnet.penumbra.zone", - "images": [ - { - "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" - } - ] - } - ], - "frontends": [ - "https://app.testnet.penumbra.zone" - ], "ibcConnections": [ { "displayName": "Osmosis", diff --git a/input/penumbra-testnet-deimos-8.json b/input/chains/penumbra-testnet-deimos-8.json similarity index 94% rename from input/penumbra-testnet-deimos-8.json rename to input/chains/penumbra-testnet-deimos-8.json index 86d1350..0132f2c 100644 --- a/input/penumbra-testnet-deimos-8.json +++ b/input/chains/penumbra-testnet-deimos-8.json @@ -1,19 +1,5 @@ { "chainId": "penumbra-testnet-deimos-8", - "rpcs": [ - { - "name": "Penumbra Labs Testnet RPC", - "url": "https://grpc.testnet.penumbra.zone", - "images": [ - { - "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" - } - ] - } - ], - "frontends": [ - "https://app.testnet.penumbra.zone" - ], "ibcConnections": [ { "displayName": "Osmosis", diff --git a/input/globals.json b/input/globals.json new file mode 100644 index 0000000..79011ca --- /dev/null +++ b/input/globals.json @@ -0,0 +1,16 @@ +{ + "rpcs": [ + { + "name": "Penumbra Labs Testnet RPC", + "url": "https://grpc.testnet.penumbra.zone", + "images": [ + { + "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" + } + ] + } + ], + "frontends": [ + "https://app.testnet.penumbra.zone" + ] +} diff --git a/npm/CHANGELOG.md b/npm/CHANGELOG.md index f3dc8aa..e35f871 100644 --- a/npm/CHANGELOG.md +++ b/npm/CHANGELOG.md @@ -1,5 +1,11 @@ # @penumbra-labs/registry +## 8.0.0 + +### Major Changes + +- New API for global rpcs & frontends + ## 7.7.0 ### Minor Changes diff --git a/npm/package.json b/npm/package.json index 08aef74..0385f64 100644 --- a/npm/package.json +++ b/npm/package.json @@ -1,6 +1,6 @@ { "name": "@penumbra-labs/registry", - "version": "7.7.0", + "version": "8.0.0", "description": "Chain and asset registry for Penumbra", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/npm/src/client.ts b/npm/src/client.ts index 828c264..2b99339 100644 --- a/npm/src/client.ts +++ b/npm/src/client.ts @@ -1,6 +1,6 @@ import { deriveTestnetChainIdFromPreview, isTestnetPreviewChainId } from './utils/testnet-parser'; import { Registry } from './registry'; -import { allJsonRegistries } from './json'; +import { allJsonRegistries, registryGlobals, RegistryGlobals } from './json'; export class ChainRegistryClient { get(chainId: string): Registry { @@ -13,6 +13,10 @@ export class ChainRegistryClient { return new Registry(jsonRegistry); } + globals(): RegistryGlobals { + return registryGlobals; + } + version() { return { commit: __COMMIT_HASH__, diff --git a/npm/src/json.ts b/npm/src/json.ts index 3e40623..071d924 100644 --- a/npm/src/json.ts +++ b/npm/src/json.ts @@ -1,14 +1,18 @@ -import * as Deimos6 from '../../registry/penumbra-testnet-deimos-6.json'; -import * as Deimos7 from '../../registry/penumbra-testnet-deimos-7.json'; -import * as Deimos8 from '../../registry/penumbra-testnet-deimos-8.json'; +import * as Deimos6 from '../../registry/chains/penumbra-testnet-deimos-6.json'; +import * as Deimos7 from '../../registry/chains/penumbra-testnet-deimos-7.json'; +import * as Deimos8 from '../../registry/chains/penumbra-testnet-deimos-8.json'; +import * as GlobalsJson from '../../registry/globals.json'; import { Base64AssetId, Chain, Rpc } from './registry'; +export interface RegistryGlobals { + rpcs: Rpc[]; + frontends: string[]; +} + export interface JsonRegistry { chainId: string; ibcConnections: Chain[]; - rpcs: Rpc[]; - frontends: string[]; assetById: Record; stakingAssetId: Base64AssetId; numeraires: Base64AssetId[]; @@ -44,3 +48,5 @@ export const allJsonRegistries: Record = { 'penumbra-testnet-deimos-7': Deimos7, 'penumbra-testnet-deimos-8': Deimos8, }; + +export const registryGlobals = GlobalsJson; diff --git a/npm/src/registry.test.ts b/npm/src/registry.test.ts index 12061b1..874a853 100644 --- a/npm/src/registry.test.ts +++ b/npm/src/registry.test.ts @@ -20,18 +20,6 @@ const testRegistry: JsonRegistry = { ], }, ], - rpcs: [ - { - name: 'Penumbra Labs Testnet RPC', - url: 'https://grpc.testnet.penumbra.zone', - images: [ - { - png: 'https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png', - }, - ], - }, - ], - frontends: ['https://app.testnet.penumbra.zone'], assetById: { 'KeqcLzNx9qSH5+lcJHBB9KNW+YPrBk5dKzvPMiypahA=': { denomUnits: [ @@ -106,10 +94,4 @@ describe('Registry', () => { const res = registry.getAllAssets(); expect(res.length).toEqual(2); }); - - it('frontends are accessible', () => { - const registry = new Registry(testRegistry); - expect(registry.frontends.length).toEqual(1); - expect(registry.frontends[0]).toEqual('https://app.testnet.penumbra.zone'); - }); }); diff --git a/npm/src/registry.ts b/npm/src/registry.ts index def27b1..9a65067 100644 --- a/npm/src/registry.ts +++ b/npm/src/registry.ts @@ -36,8 +36,6 @@ export interface Image { export class Registry { public readonly chainId: string; public readonly ibcConnections: Chain[]; - public readonly rpcs: Rpc[]; - public readonly frontends: string[]; public readonly stakingAssetId: AssetId; public readonly numeraires: AssetId[]; @@ -46,8 +44,6 @@ export class Registry { constructor(r: JsonRegistry) { this.chainId = r.chainId; this.ibcConnections = r.ibcConnections; - this.rpcs = r.rpcs; - this.frontends = r.frontends; this.assetById = mapObjectValues(r.assetById, jsonMetadata => Metadata.fromJson(jsonMetadata as unknown as JsonValue), ); diff --git a/registry/penumbra-testnet-deimos-6.json b/registry/chains/penumbra-testnet-deimos-6.json similarity index 97% rename from registry/penumbra-testnet-deimos-6.json rename to registry/chains/penumbra-testnet-deimos-6.json index 8a94734..718186f 100644 --- a/registry/penumbra-testnet-deimos-6.json +++ b/registry/chains/penumbra-testnet-deimos-6.json @@ -26,20 +26,6 @@ ] } ], - "frontends": [ - "https://app.testnet.penumbra.zone" - ], - "rpcs": [ - { - "name": "Penumbra Labs Testnet RPC", - "url": "https://grpc.testnet.penumbra.zone", - "images": [ - { - "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" - } - ] - } - ], "assetById": { "+jDercxZxs90BjC91PrWyA53/p7uN3ZcSJj3N0mHjhI=": { "description": "USDLR is a fiat-backed stablecoin issued by Stable. Stable pays DeFi protocols who distribute USDLR.", diff --git a/registry/penumbra-testnet-deimos-7.json b/registry/chains/penumbra-testnet-deimos-7.json similarity index 97% rename from registry/penumbra-testnet-deimos-7.json rename to registry/chains/penumbra-testnet-deimos-7.json index a02becd..50513dc 100644 --- a/registry/penumbra-testnet-deimos-7.json +++ b/registry/chains/penumbra-testnet-deimos-7.json @@ -26,20 +26,6 @@ ] } ], - "frontends": [ - "https://app.testnet.penumbra.zone" - ], - "rpcs": [ - { - "name": "Penumbra Labs Testnet RPC", - "url": "https://grpc.testnet.penumbra.zone", - "images": [ - { - "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" - } - ] - } - ], "assetById": { "+jDercxZxs90BjC91PrWyA53/p7uN3ZcSJj3N0mHjhI=": { "description": "USDLR is a fiat-backed stablecoin issued by Stable. Stable pays DeFi protocols who distribute USDLR.", diff --git a/registry/penumbra-testnet-deimos-8.json b/registry/chains/penumbra-testnet-deimos-8.json similarity index 97% rename from registry/penumbra-testnet-deimos-8.json rename to registry/chains/penumbra-testnet-deimos-8.json index c75a7d3..eceadb5 100644 --- a/registry/penumbra-testnet-deimos-8.json +++ b/registry/chains/penumbra-testnet-deimos-8.json @@ -26,20 +26,6 @@ ] } ], - "frontends": [ - "https://app.testnet.penumbra.zone" - ], - "rpcs": [ - { - "name": "Penumbra Labs Testnet RPC", - "url": "https://grpc.testnet.penumbra.zone", - "images": [ - { - "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" - } - ] - } - ], "assetById": { "/5AHh95RAybBbUhQ5zXMWCvstH4rRK/5KMVIVGQltAw=": { "denomUnits": [ diff --git a/registry/globals.json b/registry/globals.json new file mode 100644 index 0000000..4fc5296 --- /dev/null +++ b/registry/globals.json @@ -0,0 +1,16 @@ +{ + "rpcs": [ + { + "name": "Penumbra Labs Testnet RPC", + "url": "https://grpc.testnet.penumbra.zone", + "images": [ + { + "png": "https://raw.githubusercontent.com/prax-wallet/registry/main/images/penumbra-favicon.png" + } + ] + } + ], + "frontends": [ + "https://app.testnet.penumbra.zone" + ] +} \ No newline at end of file diff --git a/tools/compiler/Cargo.lock b/tools/compiler/Cargo.lock index af6351e..e2b768c 100644 --- a/tools/compiler/Cargo.lock +++ b/tools/compiler/Cargo.lock @@ -651,8 +651,8 @@ dependencies = [ [[package]] name = "decaf377-fmd" -version = "0.77.1" -source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.1#13f81d0792ffd1b03b7ac169544f1d3663013272" +version = "0.77.2" +source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.2#76aab4b843f47b7ce9d7177196fdd2495ccf7bfa" dependencies = [ "ark-ff", "ark-serialize", @@ -2042,8 +2042,8 @@ dependencies = [ [[package]] name = "penumbra-asset" -version = "0.77.1" -source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.1#13f81d0792ffd1b03b7ac169544f1d3663013272" +version = "0.77.2" +source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.2#76aab4b843f47b7ce9d7177196fdd2495ccf7bfa" dependencies = [ "anyhow", "ark-ff", @@ -2080,8 +2080,8 @@ dependencies = [ [[package]] name = "penumbra-num" -version = "0.77.1" -source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.1#13f81d0792ffd1b03b7ac169544f1d3663013272" +version = "0.77.2" +source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.2#76aab4b843f47b7ce9d7177196fdd2495ccf7bfa" dependencies = [ "anyhow", "ark-ff", @@ -2116,8 +2116,8 @@ dependencies = [ [[package]] name = "penumbra-proto" -version = "0.77.1" -source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.1#13f81d0792ffd1b03b7ac169544f1d3663013272" +version = "0.77.2" +source = "git+https://github.com/penumbra-zone/penumbra.git?tag=v0.77.2#76aab4b843f47b7ce9d7177196fdd2495ccf7bfa" dependencies = [ "anyhow", "async-trait", diff --git a/tools/compiler/Cargo.toml b/tools/compiler/Cargo.toml index 6051522..6d1ab3f 100644 --- a/tools/compiler/Cargo.toml +++ b/tools/compiler/Cargo.toml @@ -4,8 +4,8 @@ version = "1.0.0" edition = "2021" [dependencies] -penumbra-asset = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.77.1", package = "penumbra-asset" } -penumbra-proto = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.77.1", package = "penumbra-proto", default-features = false } +penumbra-asset = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.77.2", package = "penumbra-asset" } +penumbra-proto = { git = "https://github.com/penumbra-zone/penumbra.git", tag = "v0.77.2", package = "penumbra-proto", default-features = false } anyhow = "1.0.86" futures = "0.3.30" diff --git a/tools/compiler/src/parser.rs b/tools/compiler/src/parser.rs index a16e7d7..6d747f9 100644 --- a/tools/compiler/src/parser.rs +++ b/tools/compiler/src/parser.rs @@ -9,10 +9,15 @@ use crate::error::AppResult; #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -pub struct ChainConfig { - pub chain_id: String, +pub struct Globals { pub rpcs: Vec, pub frontends: Vec, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ChainConfig { + pub chain_id: String, pub validators: Vec, pub ibc_connections: Vec, pub native_assets: Vec, @@ -83,15 +88,9 @@ pub const LOCAL_INPUT_DIR: &str = "../../input"; /// input/ /// ├── penumbra-testnet-deimos-6.json /// └── mars-1.json -pub fn get_chain_configs(registry_dir: &str, input_dir: &str) -> AppResult> { - // Clear registry output dir - let registry_dir = Path::new(registry_dir); - if registry_dir.exists() { - fs::remove_dir_all(registry_dir)?; - } - fs::create_dir_all(registry_dir)?; - - let chain_configs = fs::read_dir(input_dir)?; +pub fn get_chain_configs(input_dir: &str) -> AppResult> { + let input_path = Path::new(input_dir).join("chains"); + let chain_configs = fs::read_dir(input_path)?; Ok(chain_configs .into_iter() .map(|input| -> AppResult { @@ -109,3 +108,43 @@ pub fn get_chain_configs(registry_dir: &str, input_dir: &str) -> AppResult AppResult<()> { + let input_path = Path::new(input_dir).join("globals.json"); + let json_data = fs::read_to_string(input_path)?; + let globals: Globals = serde_json::from_str(&json_data)?; + + // Write the validated JSON data to the output file + let output_path = Path::new(registry_dir).join("globals.json"); + let output_json = serde_json::to_string_pretty(&globals)?; + fs::write(output_path, output_json)?; + + Ok(()) +} + +// Deletes and re-creates registry dir +pub fn reset_registry_dir(path: &str) -> AppResult<()> { + let dir_path = Path::new(path); + + // Create the directory if it doesn't exist + if !dir_path.exists() { + fs::create_dir_all(dir_path)?; + } + + // Remove all the contents of the directory + for entry in fs::read_dir(dir_path)? { + let entry_path = entry?.path(); + if entry_path.is_dir() { + fs::remove_dir_all(entry_path)?; + } else { + fs::remove_file(entry_path)?; + } + } + + // Create the "chains" directory inside + let chains_dir = dir_path.join("chains"); + fs::create_dir(chains_dir)?; + + Ok(()) +} diff --git a/tools/compiler/src/processor.rs b/tools/compiler/src/processor.rs index 2f4c7b3..e94d902 100644 --- a/tools/compiler/src/processor.rs +++ b/tools/compiler/src/processor.rs @@ -12,7 +12,8 @@ use tokio::task; use crate::error::AppResult; use crate::github::assetlist_schema::AssetTypeAsset; use crate::parser::{ - get_chain_configs, ChainConfig, IbcInput, Image, Rpc, LOCAL_INPUT_DIR, LOCAL_REGISTRY_DIR, + copy_globals, get_chain_configs, reset_registry_dir, ChainConfig, IbcInput, Image, + LOCAL_INPUT_DIR, LOCAL_REGISTRY_DIR, }; use crate::querier::query_github_assets; use crate::validator::generate_metadata_from_validators; @@ -46,18 +47,18 @@ impl From for Chain { pub struct Registry { pub chain_id: String, pub ibc_connections: Vec, - pub frontends: Vec, - pub rpcs: Vec, pub asset_by_id: BTreeMap, // Using a BTreeMap to have sorted (deterministic) output pub staking_asset_id: String, pub numeraires: Vec, } pub async fn generate_registry() -> AppResult<()> { - let mut tasks = Vec::new(); + reset_registry_dir(LOCAL_REGISTRY_DIR)?; + copy_globals(LOCAL_INPUT_DIR, LOCAL_REGISTRY_DIR)?; // Get local configs from /input directory - let chain_configs = get_chain_configs(LOCAL_REGISTRY_DIR, LOCAL_INPUT_DIR)?; + let chain_configs = get_chain_configs(LOCAL_INPUT_DIR)?; + let mut tasks = Vec::new(); chain_configs.into_iter().for_each(|c| { // Async fetch metadata for ibc assets from cosmos registry let task = task::spawn(async move { process_chain_config(c).await }); @@ -68,7 +69,7 @@ pub async fn generate_registry() -> AppResult<()> { for task in tasks { let registry = task.await??; let file_name = format!("{}.json", registry.chain_id); - let output_path = Path::new(LOCAL_REGISTRY_DIR).join(file_name); + let output_path = Path::new(LOCAL_REGISTRY_DIR).join("chains").join(file_name); let output_json = serde_json::to_string_pretty(®istry)?; fs::write(output_path, output_json)?; } @@ -143,8 +144,6 @@ async fn process_chain_config(chain_config: ChainConfig) -> AppResult Ok(Registry { chain_id: chain_config.chain_id, - rpcs: chain_config.rpcs, - frontends: chain_config.frontends, ibc_connections: chain_config .ibc_connections .into_iter() diff --git a/tools/compiler/tests/test_copy_globals.rs b/tools/compiler/tests/test_copy_globals.rs new file mode 100644 index 0000000..acae9e3 --- /dev/null +++ b/tools/compiler/tests/test_copy_globals.rs @@ -0,0 +1,71 @@ +use penumbra_registry::parser::{copy_globals, Globals, Rpc}; +use std::fs::{self, File}; +use std::io::Write; +use std::path::Path; +use tempdir::TempDir; + +fn create_file_with_content(dir: &Path, file_name: &str, content: &str) { + let file_path = dir.join(file_name); + let mut file = File::create(file_path).unwrap(); + writeln!(file, "{}", content).unwrap(); +} + +#[test] +fn test_successful_copy() { + let temp_input_dir = TempDir::new("").unwrap(); + let temp_output_dir = TempDir::new("").unwrap(); + let globals = Globals { + rpcs: vec![Rpc { + name: "cybernetics".to_string(), + url: "http://api.zone".to_string(), + images: vec![], + }], + frontends: vec!["http://frontend.zone".to_string()], + }; + create_file_with_content( + temp_input_dir.path(), + "globals.json", + &serde_json::to_string(&globals).unwrap(), + ); + + assert!(copy_globals( + temp_input_dir.path().to_str().unwrap(), + temp_output_dir.path().to_str().unwrap() + ) + .is_ok()); + + let output_path = temp_output_dir.path().join("globals.json"); + let output_contents = fs::read_to_string(output_path).unwrap(); + let output_globals: Globals = serde_json::from_str(&output_contents).unwrap(); + + assert_eq!(output_globals, globals); +} + +#[test] +fn test_file_not_found() { + let temp_input_dir = TempDir::new("").unwrap(); + let temp_output_dir = TempDir::new("").unwrap(); + + assert!(copy_globals( + temp_input_dir.path().to_str().unwrap(), + temp_output_dir.path().to_str().unwrap() + ) + .is_err()); +} + +#[test] +fn test_invalid_json_format() { + let temp_input_dir = TempDir::new("").unwrap(); + let temp_output_dir = TempDir::new("").unwrap(); + create_file_with_content( + temp_input_dir.path(), + "globals.json", + "This is not valid JSON", + ); + + assert!(copy_globals( + temp_input_dir.path().to_str().unwrap(), + temp_output_dir.path().to_str().unwrap() + ) + .is_err()); +} diff --git a/tools/compiler/tests/test_parser.rs b/tools/compiler/tests/test_get_chain_configs.rs similarity index 76% rename from tools/compiler/tests/test_parser.rs rename to tools/compiler/tests/test_get_chain_configs.rs index 696d795..a37682c 100644 --- a/tools/compiler/tests/test_parser.rs +++ b/tools/compiler/tests/test_get_chain_configs.rs @@ -5,7 +5,10 @@ use std::path::Path; use tempdir::TempDir; fn create_test_config_file(dir: &Path, file_name: &str, contents: &str) { - let file_path = dir.join(file_name); + let chains_dir = dir.join("chains"); + std::fs::create_dir_all(&chains_dir).unwrap(); + + let file_path = chains_dir.join(file_name); let mut file = File::create(file_path).unwrap(); file.write_all(contents.as_bytes()).unwrap(); } @@ -13,7 +16,6 @@ fn create_test_config_file(dir: &Path, file_name: &str, contents: &str) { #[test] fn test_get_chain_configs_reads_configs_correctly() { let temp_input_dir = TempDir::new("test_input").unwrap(); - let temp_registry_dir = TempDir::new("test_registry").unwrap(); let config_content = serde_json::json!({ "chainId": "test-chain-1", @@ -27,11 +29,7 @@ fn test_get_chain_configs_reads_configs_correctly() { .to_string(); create_test_config_file(temp_input_dir.path(), "test-chain-1.json", &config_content); - let configs = get_chain_configs( - temp_registry_dir.path().to_str().unwrap(), - temp_input_dir.path().to_str().unwrap(), - ) - .unwrap(); + let configs = get_chain_configs(temp_input_dir.path().to_str().unwrap()).unwrap(); assert_eq!(configs.len(), 1); assert_eq!(configs[0].chain_id, "test-chain-1"); } @@ -39,7 +37,6 @@ fn test_get_chain_configs_reads_configs_correctly() { #[test] fn test_get_chain_configs_reads_multiple_configs_correctly() { let temp_input_dir = TempDir::new("test_input").unwrap(); - let temp_registry_dir = TempDir::new("test_registry").unwrap(); let config_content_1 = serde_json::json!({ "chainId": "test-chain-1", @@ -75,11 +72,7 @@ fn test_get_chain_configs_reads_multiple_configs_correctly() { &config_content_2, ); - let configs = get_chain_configs( - temp_registry_dir.path().to_str().unwrap(), - temp_input_dir.path().to_str().unwrap(), - ) - .unwrap(); + let configs = get_chain_configs(temp_input_dir.path().to_str().unwrap()).unwrap(); assert_eq!(configs.len(), 2); assert!(configs @@ -93,7 +86,6 @@ fn test_get_chain_configs_reads_multiple_configs_correctly() { #[test] fn test_get_chain_configs_handles_invalid_json() { let temp_input_dir = TempDir::new("test_input").unwrap(); - let temp_registry_dir = TempDir::new("test_registry").unwrap(); let invalid_config_content = "{ invalid json }"; create_test_config_file( @@ -102,11 +94,7 @@ fn test_get_chain_configs_handles_invalid_json() { invalid_config_content, ); - let configs = get_chain_configs( - temp_registry_dir.path().to_str().unwrap(), - temp_input_dir.path().to_str().unwrap(), - ) - .unwrap(); + let configs = get_chain_configs(temp_input_dir.path().to_str().unwrap()).unwrap(); assert_eq!(configs.len(), 0); } diff --git a/tools/compiler/tests/test_reset_registry_dir.rs b/tools/compiler/tests/test_reset_registry_dir.rs new file mode 100644 index 0000000..2f1944b --- /dev/null +++ b/tools/compiler/tests/test_reset_registry_dir.rs @@ -0,0 +1,46 @@ +use penumbra_registry::parser::reset_registry_dir; +use std::fs::{self, File}; +use std::io::Write; +use std::path::Path; +use tempdir::TempDir; + +fn create_file_in_dir(dir: &Path, file_name: &str) { + let file_path = dir.join(file_name); + let mut file = File::create(file_path).unwrap(); + writeln!(file, "Test content").unwrap(); +} + +fn create_subdir_in_dir(dir: &Path, subdir_name: &str) { + let subdir_path = dir.join(subdir_name); + fs::create_dir_all(subdir_path).unwrap(); +} + +#[test] +fn test_reset_non_existent_directory() { + let temp_dir = TempDir::new("").unwrap(); + let non_existent_dir = temp_dir.path().join("nonexistent"); + + reset_registry_dir(non_existent_dir.to_str().unwrap()).unwrap(); + assert!(non_existent_dir.exists()); + assert!(non_existent_dir.join("chains").exists()); +} + +#[test] +fn test_reset_existing_directory_with_files() { + let temp_dir = TempDir::new("").unwrap(); + create_file_in_dir(temp_dir.path(), "testfile.txt"); + + reset_registry_dir(temp_dir.path().to_str().unwrap()).unwrap(); + assert!(temp_dir.path().join("chains").exists()); + assert!(!temp_dir.path().join("testfile.txt").exists()); +} + +#[test] +fn test_reset_existing_directory_with_subdirectories() { + let temp_dir = TempDir::new("").unwrap(); + create_subdir_in_dir(temp_dir.path(), "subdir"); + + reset_registry_dir(temp_dir.path().to_str().unwrap()).unwrap(); + assert!(temp_dir.path().join("chains").exists()); + assert!(!temp_dir.path().join("subdir").exists()); +}