Skip to content

Commit

Permalink
Allow for name + version in Trunk.toml for PGRX extensions (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianstanton authored Jul 13, 2023
1 parent 4152094 commit 4f554a7
Show file tree
Hide file tree
Showing 24 changed files with 416 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pg-trunk"
version = "0.8.2"
version = "0.8.3"
edition = "2021"
authors = ["Steven Miller", "Ian Stanton"]
description = "A package manager for PostgreSQL extensions"
Expand Down
35 changes: 33 additions & 2 deletions cli/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,39 @@ impl SubCommand for BuildCommand {
let dependencies = cargo_toml.get("dependencies").unwrap().as_table().unwrap();
if dependencies.contains_key("pgrx") {
println!("Detected that we are building a pgrx extension");
if build_settings.version.is_some() || build_settings.name.is_some() {
return Err(anyhow!("--version and --name are collected from Cargo.toml when building pgrx extensions, please do not configure"));
// if user provides name, check that it matches Cargo.toml name
if build_settings.name.is_some() {
let package = cargo_toml.get("package");
let cargo_name = package.unwrap().get("name");
if build_settings.name
!= Some(cargo_name.unwrap().as_str().unwrap().to_string())
{
return Err(anyhow!(
"User-provided name must match name in Cargo.toml\n \
User-provided name: {}\n \
Cargo.toml name: {}\n\
",
build_settings.name.unwrap(),
cargo_name.unwrap().as_str().unwrap().to_string()
));
}
}
// if user provides version, check that it matches Cargo.toml version
if build_settings.version.is_some() {
let package = cargo_toml.get("package");
let cargo_version = package.unwrap().get("version");
if build_settings.version
!= Some(cargo_version.unwrap().as_str().unwrap().to_string())
{
return Err(anyhow!(
"User-provided version must match version in Cargo.toml\n \
User-provided version: {}\n \
Cargo.toml version: {}\n\
",
build_settings.version.unwrap(),
cargo_version.unwrap().as_str().unwrap().to_string()
));
}
}

build_pgrx(
Expand Down
137 changes: 136 additions & 1 deletion cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn install_manifest_v1_extension() -> Result<(), Box<dyn std::error::Error>> {
#[test]
fn build_pgrx_extension() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let output_dir = format!("/tmp/pgmq_test_{}", rng.gen_range(0..1000000));
let output_dir = format!("/tmp/test_pgrx_{}", rng.gen_range(0..1000000));

// Construct a path relative to the current file's directory
let mut extension_path = std::path::PathBuf::from(file!());
Expand Down Expand Up @@ -96,6 +96,52 @@ fn build_pgrx_extension() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn build_pgrx_extension_bad_name() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let output_dir = format!("/tmp/test_pgrx_{}", rng.gen_range(0..1000000));

// Construct a path relative to the current file's directory
let mut extension_path = std::path::PathBuf::from(file!());
extension_path.pop(); // Remove the file name from the path
extension_path.push("test_pgrx_extension");

let mut cmd = Command::cargo_bin(CARGO_BIN)?;
cmd.arg("build");
cmd.arg("--path");
cmd.arg(extension_path.as_os_str());
cmd.arg("--name");
cmd.arg("bad_name");
cmd.arg("--output-path");
cmd.arg(output_dir.clone());
cmd.assert().code(101);

Ok(())
}

#[test]
fn build_pgrx_extension_bad_version() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let output_dir = format!("/tmp/test_pgrx_{}", rng.gen_range(0..1000000));

// Construct a path relative to the current file's directory
let mut extension_path = std::path::PathBuf::from(file!());
extension_path.pop(); // Remove the file name from the path
extension_path.push("test_pgrx_extension");

let mut cmd = Command::cargo_bin(CARGO_BIN)?;
cmd.arg("build");
cmd.arg("--path");
cmd.arg(extension_path.as_os_str());
cmd.arg("--version");
cmd.arg("0.0.1");
cmd.arg("--output-path");
cmd.arg(output_dir.clone());
cmd.assert().code(101);

Ok(())
}

#[test]
fn build_c_extension() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
Expand Down Expand Up @@ -324,3 +370,92 @@ fn build_pg_cron_trunk_toml() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[test]
fn build_pgrx_with_trunk_toml() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let output_dir = format!(
"/tmp/test_pgrx_with_trunk_toml_{}",
rng.gen_range(0..1000000)
);

// Construct a path relative to the current file's directory
let mut trunkfile_path = std::path::PathBuf::from(file!());
trunkfile_path.pop(); // Remove the file name from the path
trunkfile_path.push("test_trunk_toml_dirs");
trunkfile_path.push("pgrx_with_trunk_toml");

let mut cmd = Command::cargo_bin(CARGO_BIN)?;
cmd.arg("build");
cmd.arg("--path");
cmd.arg(trunkfile_path.as_os_str());
cmd.arg("--output-path");
cmd.arg(output_dir.clone());
cmd.assert().code(0);
assert!(std::path::Path::new(
format!("{output_dir}/test_pgrx_extension-0.0.0.tar.gz").as_str()
)
.exists());
// assert any license files are included
let output = Command::new("tar")
.arg("-tvf")
.arg(format!("{output_dir}/test_pgrx_extension-0.0.0.tar.gz").as_str())
.output()
.expect("failed to run tar command");
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(stdout.contains("licenses/LICENSE.txt"));
// delete the temporary file
std::fs::remove_dir_all(output_dir)?;

Ok(())
}

#[test]
fn build_pgrx_with_trunk_toml_bad_name() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let output_dir = format!(
"/tmp/test_pgrx_with_trunk_toml_bad_name_{}",
rng.gen_range(0..1000000)
);

// Construct a path relative to the current file's directory
let mut trunkfile_path = std::path::PathBuf::from(file!());
trunkfile_path.pop(); // Remove the file name from the path
trunkfile_path.push("test_trunk_toml_dirs");
trunkfile_path.push("pgrx_with_trunk_toml_bad_name");

let mut cmd = Command::cargo_bin(CARGO_BIN)?;
cmd.arg("build");
cmd.arg("--path");
cmd.arg(trunkfile_path.as_os_str());
cmd.arg("--output-path");
cmd.arg(output_dir.clone());
cmd.assert().code(101);

Ok(())
}

#[test]
fn build_pgrx_with_trunk_toml_bad_version() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let output_dir = format!(
"/tmp/test_pgrx_with_trunk_toml_bad_version_{}",
rng.gen_range(0..1000000)
);

// Construct a path relative to the current file's directory
let mut trunkfile_path = std::path::PathBuf::from(file!());
trunkfile_path.pop(); // Remove the file name from the path
trunkfile_path.push("test_trunk_toml_dirs");
trunkfile_path.push("pgrx_with_trunk_toml_bad_version");

let mut cmd = Command::cargo_bin(CARGO_BIN)?;
cmd.arg("build");
cmd.arg("--path");
cmd.arg(trunkfile_path.as_os_str());
cmd.arg("--output-path");
cmd.arg(output_dir.clone());
cmd.assert().code(101);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.'cfg(target_os="macos")']
# Postgres symbols won't be available until runtime
rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.idea/
/target
*.iml
**/*.rs.bk
Cargo.lock
Dockerfile
29 changes: 29 additions & 0 deletions cli/tests/test_trunk_toml_dirs/pgrx_with_trunk_toml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "test_pgrx_extension"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[features]
default = ["pg15"]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15" ]
pg_test = []

[dependencies]
pgrx = "=0.8.0"

[dev-dependencies]
pgrx-tests = "=0.8.0"

[profile.dev]
panic = "unwind"
lto = "thin"

[profile.release]
panic = "unwind"
opt-level = 3
lto = "fat"
codegen-units = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test license file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[extension]
name = "test_pgrx_extension"
version = "0.0.0"
34 changes: 34 additions & 0 deletions cli/tests/test_trunk_toml_dirs/pgrx_with_trunk_toml/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use pgrx::prelude::*;

pgrx::pg_module_magic!();

#[pg_extern]
fn hello_test_extension() -> &'static str {
"Hello, test_extension"
}

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgrx::prelude::*;

#[pg_test]
fn test_hello_test_extension() {
assert_eq!("Hello, test_extension", crate::hello_test_extension());
}

}

/// This module is required by `cargo pgrx test` invocations.
/// It must be visible at the root of your extension crate.
#[cfg(test)]
pub mod pg_test {
pub fn setup(_options: Vec<&str>) {
// perform one-off initialization when the pg_test framework starts
}

pub fn postgresql_conf_options() -> Vec<&'static str> {
// return any postgresql.conf settings that are required for your tests
vec![]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
comment = 'test_pgrx_extension: Created by pgrx'
default_version = '@CARGO_VERSION@'
module_pathname = '$libdir/test_pgrx_extension'
relocatable = false
superuser = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.'cfg(target_os="macos")']
# Postgres symbols won't be available until runtime
rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.idea/
/target
*.iml
**/*.rs.bk
Cargo.lock
Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "test_pgrx_extension"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[features]
default = ["pg15"]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15" ]
pg_test = []

[dependencies]
pgrx = "=0.8.0"

[dev-dependencies]
pgrx-tests = "=0.8.0"

[profile.dev]
panic = "unwind"
lto = "thin"

[profile.release]
panic = "unwind"
opt-level = 3
lto = "fat"
codegen-units = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test license file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[extension]
name = "wrong_name"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use pgrx::prelude::*;

pgrx::pg_module_magic!();

#[pg_extern]
fn hello_test_extension() -> &'static str {
"Hello, test_extension"
}

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgrx::prelude::*;

#[pg_test]
fn test_hello_test_extension() {
assert_eq!("Hello, test_extension", crate::hello_test_extension());
}

}

/// This module is required by `cargo pgrx test` invocations.
/// It must be visible at the root of your extension crate.
#[cfg(test)]
pub mod pg_test {
pub fn setup(_options: Vec<&str>) {
// perform one-off initialization when the pg_test framework starts
}

pub fn postgresql_conf_options() -> Vec<&'static str> {
// return any postgresql.conf settings that are required for your tests
vec![]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
comment = 'test_pgrx_extension: Created by pgrx'
default_version = '@CARGO_VERSION@'
module_pathname = '$libdir/test_pgrx_extension'
relocatable = false
superuser = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.'cfg(target_os="macos")']
# Postgres symbols won't be available until runtime
rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.idea/
/target
*.iml
**/*.rs.bk
Cargo.lock
Dockerfile
Loading

0 comments on commit 4f554a7

Please sign in to comment.