Skip to content

Commit

Permalink
feat(hooks): check component is defined inside the correct place
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingandyyy committed Jun 24, 2024
1 parent 2722c12 commit 9c81fe9
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 2 deletions.
Empty file added docs/modules/component.md
Empty file.
67 changes: 67 additions & 0 deletions hooks/domain-component-valididate/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash -e

# Function to check if module names follow the 'component_<name>' convention
check_component_module_id() {
local module_name="$1"
if [[ ! "$module_name" =~ ^component_ ]]; then
echo "ERROR: Module name '$module_name' does not follow the 'component_<name>' naming convention."
return 1
fi
return 0
}

# Function to check if the filename matches the expected filename derived from the module 'name' attribute
check_component_file_name() {
local module_name="$1"
local component_name="$2"
local file="$3"
local filename
local expected_file
local parent_directory
local parent_directory_name

filename=$(basename "$file")
expected_file="${component_name//_/-}.tf"
parent_directory=$(dirname "$file")
parent_directory_name=$(basename "$parent_directory")

if [[ "$filename" != "$expected_file" && ("$filename" != "main.tf" || ! "$parent_directory_name" =~ -component$) ]]; then
echo "ERROR: Module '$module_name' with name '$component_name' should be in '$expected_file' or a 'main.tf' within a directory ending with '-component', but found in '$filename'."
return 1
fi
return 0
}

check_component_files() {
has_error=0
for file in "$@"; do
# Check only '.tf' files to avoid processing other file types
if [[ "$file" == *.tf ]]; then
# Read through the file line by line to find module blocks and extract the 'name' field
module_name=""
while IFS= read -r line || [[ -n "$line" ]]; do
# Check if the line declares a module
if [[ "$line" =~ ^module\ \" ]]; then
module_name=$(echo "$line" | awk -F\" '{print $2}')
check_component_module_id "$module_name" || has_error=1
fi
# When module_name is set, look for the name attribute within the block
if [[ -n "$module_name" && "$line" =~ ^\ *name\ *= ]]; then
component_name=$(echo "$line" | awk -F\" '{print $2}')
check_component_file_name "$module_name" "$component_name" "$file" || has_error=1
# Reset module_name to ensure correct block processing
module_name=""
fi
done < <(cat "$file")
fi
done
return $has_error
}

# Execute the check across all .tf files passed as arguments
if ! check_component_files "$@"; then
echo "components defined in the files that do not match our naming convention: '<component-name>.tf' or 'main.tf' within a directory ending with '-component'."
echo "See: https://open-turo.github.io/standards-terraform/modules/component/"
fi

exit $has_error
7 changes: 7 additions & 0 deletions hooks/domain-component-valididate/fixtures/bad-component.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "foo_bar_component" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
7 changes: 7 additions & 0 deletions hooks/domain-component-valididate/fixtures/bad-folder/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "component_good_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "good-foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
7 changes: 7 additions & 0 deletions hooks/domain-component-valididate/fixtures/bad-foo-bar.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "component_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "component_good_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "good-foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
7 changes: 7 additions & 0 deletions hooks/domain-component-valididate/fixtures/good-foo-bar.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "component_good_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "good-foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
35 changes: 35 additions & 0 deletions hooks/domain-component-valididate/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash -e

# get the directory of the script
script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

echo "testing: $script_directory"

echo "testing: check $script_directory/fixtures/good-foo-bar.tf"
"$script_directory/check" "$script_directory/fixtures/good-foo-bar.tf"

echo "testing: check $script_directory/fixtures/good-component/main.tf"
"$script_directory/check" "$script_directory/fixtures/good-component/main.tf"

echo "testing: check $script_directory/fixtures/bad-folder/main.tf"
echo " expecting error"
if "$script_directory/check" "$script_directory/fixtures/bad-folder/main.tf"; then
echo "ERROR: should have failed"
exit 1
fi

echo "testing: check $script_directory/fixtures/bad-foo-bar.tf"
echo " expecting error"
if "$script_directory/check" "$script_directory/fixtures/bad-foo-bar.tf"; then
echo "ERROR: should have failed"
exit 1
fi

echo "testing: check $script_directory/fixtures/bad-component.tf"
echo " expecting error"
if "$script_directory/check" "$script_directory/fixtures/bad-component.tf"; then
echo "ERROR: should have failed"
exit 1
fi

echo "testing: PASS"
9 changes: 7 additions & 2 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

REPO_DIR="$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)")"

"$REPO_DIR/hooks/outputs-in-outputs-files/test"
"$REPO_DIR/hooks/vars-in-variables-files/test"
# Find all files in the hooks directory and its subdirectories
find "$REPO_DIR/hooks" -type f | while read -r file; do
# If the file is a test script, execute it
if [[ "$file" == *"/test" ]]; then
"$file"
fi
done

0 comments on commit 9c81fe9

Please sign in to comment.