Skip to content

Commit

Permalink
Merge pull request #1 from loispostula/add-tests
Browse files Browse the repository at this point in the history
✅ Bootstrap tests
  • Loading branch information
loispostula authored Nov 28, 2024
2 parents 4b1a154 + f5aeb80 commit ac17017
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Tests
on:
- pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install terraform-docs
uses: jaxxstorm/[email protected]
with:
repo: terraform-docs/terraform-docs
tag: v0.19.0
- uses: purcell/setup-emacs@master
with:
version: 27.1
- uses: actions/setup-python@v2
- uses: conao3/setup-cask@master
- name: Run tests
run: |
cask install
cask exec buttercup -L . test/
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
### Emacs ###
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*

# Org-mode
.org-id-locations
*_archive

# flymake-mode
*_flymake.*

# eshell files
/eshell/history
/eshell/lastdir

# elpa packages
/elpa/

# reftex files
*.rel

# AUCTeX auto folder
/auto/

# cask packages
.cask/
dist/

# Flycheck
flycheck_*.el

# server auth directory
/server/

# projectiles files
.projectile

# directory configuration
.dir-locals.el

# network security
/network-security.data
7 changes: 7 additions & 0 deletions Cask
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(source gnu)
(source melpa)

(package-file "terraform-docs.el")

(development
(depends-on "buttercup"))
5 changes: 3 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#+title: terraform-docs.el: Generate your Terraform documentation from Emacs
#+author: Loïs Postula

[[https://github.com/loispostula/terraform-docs.el/actions][https://github.com/loispostula/terraform-docs.el/actions/workflows/test.yml/badge.svg]] [[https://melpa.org/#/terraform-docs.el][file:https://melpa.org/packages/terraform-docs.el.svg]] [[https://www.gnu.org/licenses/gpl-3.0][https://img.shields.io/badge/License-GPL%20v3-blue.svg]]

[[https://github.com/loispostula/terraform-docs.el/actions][https://github.com/loispostula/terraform-docs.el/actions/workflows/test.yml/badge.svg]]
[[https://melpa.org/#/terraform-docs.el][file:https://melpa.org/packages/terraform-docs.el.svg]]
[[https://www.gnu.org/licenses/gpl-3.0][https://img.shields.io/badge/License-GPL%20v3-blue.svg]]

* ~terraform-docs.el~
An Emacs package for seamlessly integrating [[https://terraform-docs.io/][terraform-docs]] into your workflow. This package provides utilities for generating [[https://terraform.io][Terraform]] documentation from your modules, with options to view the output in a buffer, write it to a file, or both.
Expand Down
2 changes: 1 addition & 1 deletion terraform-docs.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Author: Loïs Postula <[email protected]>
;; URL: https://github.com/loispostula/terraform-docs.el
;; Version: 0.1
;; Package-Requires: ((emacs "25.1"))
;; Package-Requires: ((emacs "27.1"))
;; Keywords: terraform, tools, docs

;; This program is free software; you can redistribute it and/or modify
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/.terraform-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
formatter: "markdown table"

settings:
hide-empty: true
13 changes: 13 additions & 0 deletions test/fixtures/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* terraform-docs.el:
*
* Example of 'test' module.
*/

terraform {
required_version = ">= 0.12"
required_providers {
random = ">= 2.2.0"
}
}
resource "null_resource" "foo" {}
89 changes: 89 additions & 0 deletions test/test-terraform-docs.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
;;; terraform-docs-spec.el --- Buttercup tests for terraform-docs.el -*- lexical-binding: t; -*-

(require 'buttercup)
(require 'terraform-docs)

(defvar terraform-docs-fixtures-dir
(expand-file-name "fixtures" (file-name-directory (or load-file-name buffer-file-name)))
"Directory containing test fixtures for terraform-docs.")

(defvar terraform-docs-expected-output
"terraform-docs.el:
Example of 'test' module.
## Requirements
| Name | Version |
|------|---------|
| <a name=\"requirement_terraform\"></a> [terraform](#requirement_terraform) | >= 0.12 |
| <a name=\"requirement_random\"></a> [random](#requirement_random) | >= 2.2.0 |
## Providers
| Name | Version |
|------|---------|
| <a name=\"provider_null\"></a> [null](#provider_null) | n/a |
## Resources
| Name | Type |
|------|------|
| [null_resource.foo](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |"
"Expected output generated by terraform-docs for the test fixtures.")
(defun terraform-docs-normalize-output (output)
"Normalize OUTPUT by unescaping escaped characters and trimming whitespace."
(let ((clean-output (replace-regexp-in-string "\\\\" "" output))) ;; Unescape characters
(string-trim clean-output))) ;; Remove leading and trailing whitespace

(describe "terraform-docs executable"
(it "is available in the system PATH"
(let ((executable (executable-find "terraform-docs")))
(expect executable :not :to-be nil)
(expect (file-executable-p executable) :to-be t))))

(describe "terraform-docs-config-file"
(it "finds the default .terraform-docs.yml file in fixtures"
(let ((config-file (terraform-docs-config-file terraform-docs-fixtures-dir)))
(expect config-file :to-equal
(expand-file-name ".terraform-docs.yml" terraform-docs-fixtures-dir))))

(it "returns nil if no configuration file is found"
(let ((terraform-docs-config-name "nonexistent-config.yml"))
(let ((config-file (terraform-docs-config-file terraform-docs-fixtures-dir)))
(expect config-file :to-be nil))))

(it "finds a custom configuration file in fixtures"
(let ((terraform-docs-config-name ".custom-docs-config.yml"))
(copy-file (expand-file-name ".terraform-docs.yml" terraform-docs-fixtures-dir)
(expand-file-name ".custom-docs-config.yml" terraform-docs-fixtures-dir) t)
(unwind-protect
(let ((config-file (terraform-docs-config-file terraform-docs-fixtures-dir)))
(expect config-file :to-equal
(expand-file-name ".custom-docs-config.yml" terraform-docs-fixtures-dir)))
(delete-file (expand-file-name ".custom-docs-config.yml" terraform-docs-fixtures-dir))))))

(describe "terraform-docs-run"
(it "generates the expected output as a string"
(let* ((output (terraform-docs-run (expand-file-name "main.tf" terraform-docs-fixtures-dir) t))
(clean-output (terraform-docs-normalize-output (substring-no-properties output))))
(expect clean-output :to-equal terraform-docs-expected-output))))

(describe "terraform-docs-to-file"
(it "creates an output file with the expected content"
(let ((output-file (terraform-docs-to-file (expand-file-name "main.tf" terraform-docs-fixtures-dir))))
(expect (file-exists-p output-file) :to-be t)
(expect (with-temp-buffer
(insert-file-contents output-file)
(terraform-docs-normalize-output (buffer-string)))
:to-equal terraform-docs-expected-output)
(delete-file output-file))))

(describe "terraform-docs-to-file-and-open"
(it "creates an output file and opens it"
(spy-on 'find-file :and-call-fake
(lambda (file)
(expect (file-name-nondirectory file) :to-match "^output-for-fixtures\\.md$")))
(let ((output-file (terraform-docs-to-file (expand-file-name "main.tf" terraform-docs-fixtures-dir))))
(terraform-docs-to-file-and-open (expand-file-name "main.tf" terraform-docs-fixtures-dir))
(delete-file output-file))))

0 comments on commit ac17017

Please sign in to comment.