fix parsing with new default comment

This commit is contained in:
Robin Appelman 2026-05-06 17:14:20 +02:00
commit 1da941262e
4 changed files with 65 additions and 2 deletions

View file

@ -1,8 +1,10 @@
use std::env::args;
use miette::Result;
use nextcloud_config_parser::parse;
fn main() -> Result<()> {
let config = parse("tests/configs/basic.php")?;
let config = parse(args().nth(1).expect("no config file provided"))?;
dbg!(config);
Ok(())
}

View file

@ -53,7 +53,7 @@ fn parse_php(path: impl AsRef<Path>) -> Result<Value> {
}
}
let php = match content.find("$CONFIG") {
let php = match content.rfind("$CONFIG") {
Some(pos) => content[pos + "$CONFIG".len()..]
.trim()
.trim_start_matches('='),

View file

@ -81,6 +81,36 @@ fn test_parse_config_basic() {
);
}
#[test]
fn test_parse_config_example_comment() {
let config = config_from_file("tests/configs/comment_example.php");
assert_eq!("https://cloud.example.com", config.nextcloud_url);
assert_eq!("oc_", config.database_prefix);
assert_debug_equal(
&Database::MySql {
database: "nextcloud".to_string(),
username: "nextcloud".to_string(),
password: "secret".to_string(),
connect: DbConnect::Tcp {
host: "127.0.0.1".to_string(),
port: 3306,
},
ssl_options: SslOptions::Disabled,
},
&config.database,
);
assert_eq!(
config.database.url(),
"mysql://nextcloud:secret@127.0.0.1/nextcloud?ssl-mode=disabled"
);
assert_debug_equal(
RedisConfig::Single(parse_redis("redis://127.0.0.1")),
config.redis,
);
}
#[test]
fn test_parse_implicit_prefix() {
let config = config_from_file("tests/configs/implicit_prefix.php");

View file

@ -0,0 +1,31 @@
<?php
/**
* WARNING
*
* This file gets modified by automatic processes and all lines that are not
* active code (ie. comments) are lost during that process.
*
* If you want to document things with comments or use constants add your settings
* in a '<NAME>.config.php' file which will be included and rendered into this file.
*
* Example:
* <?php
* $CONFIG = [];
*
* See also: https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html#multiple-merged-configuration-files
*/
$CONFIG = [
"overwrite.cli.url" => "https://cloud.example.com",
"dbtype" => "mysql",
"dbname" => "nextcloud",
"dbhost" => "127.0.0.1",
"dbport" => "",
"dbtableprefix" => "oc_",
"dbuser" => "nextcloud",
"dbpassword" => "secret",
"redis" => [
"host" => "localhost",
],
];