diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..312609b --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,46 @@ +// This file is part of the shadow-rs package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +//! Shared test helpers for shadow-rs integration tests. +//! +//! Import with `#[path = "../common/mod.rs"] mod common;` in test files. + +/// Skip the test when not running as root (euid != 0). +/// +/// Returns `true` if the test should be skipped. +pub fn skip_unless_root() -> bool { + !nix::unistd::geteuid().is_root() +} + +/// Create a temp directory with synthetic `/etc/` files for testing. +/// +/// Returns a `TempDir` — the directory and files are cleaned up on drop. +pub fn setup_prefix(shadow_content: &str) -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("failed to create temp dir"); + let etc = dir.path().join("etc"); + std::fs::create_dir_all(&etc).expect("failed to create etc dir"); + std::fs::write(etc.join("shadow"), shadow_content).expect("failed to write shadow"); + dir +} + +/// Create a temp directory with passwd, shadow, and group files. +pub fn setup_full_prefix( + passwd_content: &str, + shadow_content: &str, + group_content: &str, +) -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("failed to create temp dir"); + let etc = dir.path().join("etc"); + std::fs::create_dir_all(&etc).expect("failed to create etc dir"); + std::fs::write(etc.join("passwd"), passwd_content).expect("failed to write passwd"); + std::fs::write(etc.join("shadow"), shadow_content).expect("failed to write shadow"); + std::fs::write(etc.join("group"), group_content).expect("failed to write group"); + dir +} + +/// Read a file from a temp prefix directory. +pub fn read_file(dir: &tempfile::TempDir, relative: &str) -> String { + std::fs::read_to_string(dir.path().join(relative)).expect("failed to read file") +} diff --git a/util/generate-completions.sh b/util/generate-completions.sh new file mode 100755 index 0000000..6687bb1 --- /dev/null +++ b/util/generate-completions.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# Generate shell completions for all shadow-rs tools. +# +# Usage: +# ./util/generate-completions.sh bash +# ./util/generate-completions.sh zsh +# ./util/generate-completions.sh fish +# +# For proper clap-based completions, build with clap_complete. +# This is a minimal placeholder until clap_complete is integrated. + +set -euo pipefail + +SHELL_TYPE="${1:-bash}" +TOOLS="passwd pwck useradd userdel usermod chpasswd chage groupadd groupdel groupmod grpck chfn chsh newgrp" + +case "$SHELL_TYPE" in + bash) + for tool in $TOOLS; do + echo "complete -W '--help' $tool" + done + ;; + zsh) + echo "#compdef shadow-rs" + for tool in $TOOLS; do + echo "complete -c $tool -s h -l help -d 'Show help'" + done + ;; + fish) + for tool in $TOOLS; do + echo "complete -c $tool -s h -l help -d 'Show help'" + done + ;; + *) + echo "Usage: $0 {bash|zsh|fish}" >&2 + exit 1 + ;; +esac