add test, fix bug with missing xml header
This commit is contained in:
parent
f6e39067c8
commit
935bfb4139
5 changed files with 149 additions and 95 deletions
|
|
@ -6,6 +6,16 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn get_settings() -> Result<Settings, String> {
|
pub fn get_settings() -> Result<Settings, String> {
|
||||||
|
let settings_path = get_settings_path().map_err(|e| e.to_string())?;
|
||||||
|
get_settings_from_path(settings_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings_from_path(settings_path: PathBuf) -> Result<Settings, String> {
|
||||||
|
let settings = fs::read_to_string(settings_path).map_err(|e| e.to_string())?;
|
||||||
|
get_settings_from_string(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings_from_string(settings: String) -> Result<Settings, String> {
|
||||||
let mut local_repository = None;
|
let mut local_repository = None;
|
||||||
let mut interactive_mode = true;
|
let mut interactive_mode = true;
|
||||||
let mut use_plugin_registry = false;
|
let mut use_plugin_registry = false;
|
||||||
|
|
@ -17,14 +27,8 @@ pub fn get_settings() -> Result<Settings, String> {
|
||||||
let mut active_profiles = vec![];
|
let mut active_profiles = vec![];
|
||||||
let mut plugin_groups = vec![];
|
let mut plugin_groups = vec![];
|
||||||
|
|
||||||
let settings_path = get_settings_path();
|
let root = get_document(settings).map_err(|err| err.to_string())?.root;
|
||||||
let settings_file = fs::read_to_string(settings_path?).map_err(|e| e.to_string())?;
|
for child in root.children {
|
||||||
|
|
||||||
for child in get_document(settings_file)
|
|
||||||
.map_err(|err| err.to_string())?
|
|
||||||
.root
|
|
||||||
.children
|
|
||||||
{
|
|
||||||
match child.name.as_str() {
|
match child.name.as_str() {
|
||||||
"localRepository" => local_repository = child.text,
|
"localRepository" => local_repository = child.text,
|
||||||
"interactiveMode" => interactive_mode = child.text.map(|b| b == "true").unwrap_or(true),
|
"interactiveMode" => interactive_mode = child.text.map(|b| b == "true").unwrap_or(true),
|
||||||
|
|
@ -405,119 +409,107 @@ fn get_settings_path() -> Result<PathBuf, String> {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
local_repository: Option<String>,
|
pub local_repository: Option<String>,
|
||||||
interactive_mode: bool,
|
pub interactive_mode: bool,
|
||||||
use_plugin_registry: bool,
|
pub use_plugin_registry: bool,
|
||||||
offline: bool,
|
pub offline: bool,
|
||||||
proxies: Vec<Proxy>,
|
pub proxies: Vec<Proxy>,
|
||||||
servers: Vec<Server>,
|
pub servers: Vec<Server>,
|
||||||
mirrors: Vec<Mirror>,
|
pub mirrors: Vec<Mirror>,
|
||||||
profiles: Vec<Profile>,
|
pub profiles: Vec<Profile>,
|
||||||
active_profiles: Vec<String>,
|
pub active_profiles: Vec<String>,
|
||||||
plugin_groups: Vec<String>,
|
pub plugin_groups: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Server {
|
pub struct Server {
|
||||||
id: Option<String>,
|
pub id: Option<String>,
|
||||||
username: Option<String>,
|
pub username: Option<String>,
|
||||||
password: Option<String>,
|
pub password: Option<String>,
|
||||||
private_key: Option<String>,
|
pub private_key: Option<String>,
|
||||||
passphrase: Option<String>,
|
pub passphrase: Option<String>,
|
||||||
file_permissions: Option<String>,
|
pub file_permissions: Option<String>,
|
||||||
directory_permissions: Option<String>,
|
pub directory_permissions: Option<String>,
|
||||||
configuration: Option<Node>, //xsd:any
|
pub configuration: Option<Node>, //xsd:any
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Mirror {
|
pub struct Mirror {
|
||||||
id: Option<String>,
|
pub id: Option<String>,
|
||||||
mirror_of: Option<String>,
|
pub mirror_of: Option<String>,
|
||||||
name: Option<String>,
|
pub name: Option<String>,
|
||||||
url: Option<String>,
|
pub url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Proxy {
|
pub struct Proxy {
|
||||||
active: bool,
|
pub active: bool,
|
||||||
protocol: String,
|
pub protocol: String,
|
||||||
username: Option<String>,
|
pub username: Option<String>,
|
||||||
password: Option<String>,
|
pub password: Option<String>,
|
||||||
port: usize,
|
pub port: usize,
|
||||||
host: Option<String>,
|
pub host: Option<String>,
|
||||||
non_proxy_hosts: Option<String>,
|
pub non_proxy_hosts: Option<String>,
|
||||||
id: Option<String>,
|
pub id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Profile {
|
pub struct Profile {
|
||||||
id: Option<String>,
|
pub id: Option<String>,
|
||||||
activation: Option<Activation>,
|
pub activation: Option<Activation>,
|
||||||
properties: Vec<Property>,
|
pub properties: Vec<Property>,
|
||||||
repositories: Vec<Repository>,
|
pub repositories: Vec<Repository>,
|
||||||
plugin_repositories: Vec<Repository>,
|
pub plugin_repositories: Vec<Repository>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Activation {
|
pub struct Activation {
|
||||||
active_by_default: bool,
|
pub active_by_default: bool,
|
||||||
jdk: Option<String>,
|
pub jdk: Option<String>,
|
||||||
os: Option<ActivationOs>,
|
pub os: Option<ActivationOs>,
|
||||||
property: Option<ActivationProperty>,
|
pub property: Option<ActivationProperty>,
|
||||||
file: Option<ActivationFile>,
|
pub file: Option<ActivationFile>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ActivationOs {
|
pub struct ActivationOs {
|
||||||
name: Option<String>,
|
pub name: Option<String>,
|
||||||
family: Option<String>,
|
pub family: Option<String>,
|
||||||
arch: Option<String>,
|
pub arch: Option<String>,
|
||||||
version: Option<String>,
|
pub version: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ActivationProperty {
|
pub struct ActivationProperty {
|
||||||
name: Option<String>,
|
pub name: Option<String>,
|
||||||
value: Option<String>,
|
pub value: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ActivationFile {
|
pub struct ActivationFile {
|
||||||
missing: Option<String>,
|
pub missing: Option<String>,
|
||||||
exists: Option<String>,
|
pub exists: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Property {
|
pub struct Property {
|
||||||
name: String,
|
pub name: String,
|
||||||
value: Option<String>,
|
pub value: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Repository {
|
pub struct Repository {
|
||||||
releases: Option<RepositoryPolicy>,
|
pub releases: Option<RepositoryPolicy>,
|
||||||
snapshots: Option<RepositoryPolicy>,
|
pub snapshots: Option<RepositoryPolicy>,
|
||||||
id: Option<String>,
|
pub id: Option<String>,
|
||||||
name: Option<String>,
|
pub name: Option<String>,
|
||||||
url: Option<String>,
|
pub url: Option<String>,
|
||||||
layout: String,
|
pub layout: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct RepositoryPolicy {
|
pub struct RepositoryPolicy {
|
||||||
enabled: bool,
|
pub enabled: bool,
|
||||||
update_policy: Option<String>,
|
pub update_policy: Option<String>,
|
||||||
checksum_policy: Option<String>,
|
pub checksum_policy: Option<String>,
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test() {
|
|
||||||
let settings = get_settings().expect("no fail");
|
|
||||||
println!("{:?}", settings);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,10 @@ impl<'a> SAXParser<'a> {
|
||||||
|
|
||||||
fn parse(&mut self) -> Result<(), SaxError> {
|
fn parse(&mut self) -> Result<(), SaxError> {
|
||||||
self.advance()?;
|
self.advance()?;
|
||||||
self.expect(
|
// self.expect(
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
|
// "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
|
||||||
"Content is not allowed in prolog.",
|
// "Content is not allowed in prolog.",
|
||||||
)?;
|
// ).unwrap_or_default(); // not fatal TODO
|
||||||
self.skip_whitespace()?;
|
self.skip_whitespace()?;
|
||||||
self.handler.start_document();
|
self.handler.start_document();
|
||||||
self.parse_elements()
|
self.parse_elements()
|
||||||
|
|
@ -63,6 +63,12 @@ impl<'a> SAXParser<'a> {
|
||||||
self.char_buffer.clear();
|
self.char_buffer.clear();
|
||||||
}
|
}
|
||||||
self.advance()?;
|
self.advance()?;
|
||||||
|
if self.current == '?' {
|
||||||
|
self.expect(
|
||||||
|
"?xml version=\"1.0\" encoding=\"UTF-8\"?>",
|
||||||
|
"Content is not allowed in prolog.",
|
||||||
|
)?;
|
||||||
|
}
|
||||||
if self.current == '!' {
|
if self.current == '!' {
|
||||||
self.skip_comment()?;
|
self.skip_comment()?;
|
||||||
} else if self.current != '/' {
|
} else if self.current != '/' {
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
mod pom_parser_test;
|
mod pom_parser_test;
|
||||||
mod project_parser_test;
|
mod project_parser_test;
|
||||||
|
mod settings_test;
|
||||||
47
tests/maven/resources/settings.xml
Normal file
47
tests/maven/resources/settings.xml
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
|
||||||
|
http://maven.apache.org/xsd/settings-1.0.0.xsd">
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>github</id>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>central</id>
|
||||||
|
<url>https://repo1.maven.org/maven2</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>github</id>
|
||||||
|
<url>https://maven.pkg.github.com/shautvast/JsonToy</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>reflective</id>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>central</id>
|
||||||
|
<url>https://repo1.maven.org/maven2</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>github</id>
|
||||||
|
<url>https://maven.pkg.github.com/shautvast/reflective</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
<servers>
|
||||||
|
<server>
|
||||||
|
<id>github</id>
|
||||||
|
<username>shautvast</username>
|
||||||
|
<password>foobar</password>
|
||||||
|
</server>
|
||||||
|
</servers>
|
||||||
|
</settings>
|
||||||
8
tests/maven/settings_test.rs
Normal file
8
tests/maven/settings_test.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
use undeepend::maven::settings::get_settings_from_string;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
let settings = include_str!("../maven/resources/settings.xml").to_string();
|
||||||
|
let settings = get_settings_from_string(settings).expect("no fail");
|
||||||
|
assert!(!settings.profiles.is_empty());
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue