Repository: segmentio/terraform-docs
Pull request #224 (2020-03-24 13:52:33)

--no-header should not attempt reading main.tf file
### Prerequisites

Put an `x` into the box(es) that apply:

- [x] This pull request fixes a bug.
- [ ] This pull request adds a feature.
- [ ] This pull request enhances existing functionality.
- [ ] This pull request introduces breaking change.

For more information, see the [Contributing Guide](https://github.com/segmentio/terraform-docs/tree/master/CONTRIBUTING.md).

### Description

Our projects do not have that file. Even with `--no-header`, terraform-docs crashes. This PR stops the tool from reading a file when `--no-header` is set.
I'm not sure if the test is in the correct location. Let me know if that works for you

### Checklist

Put an `x` into all boxes that apply:

- [x] I have read the [Contributing Guidelines](https://github.com/segmentio/terraform-docs/tree/master/CONTRIBUTING.md).

#### Tests

- [x] I have added tests to cover my changes.
- [x] All tests pass when I run `make test`.

#### Documentation

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.

#### Code Style

- [x] My code follows the code style of this project.


codecov-io: # [Codecov](https://codecov.io/gh/segmentio/terraform-docs/pull/224?src=pr&el=h1) Report
> Merging [#224](https://codecov.io/gh/segmentio/terraform-docs/pull/224?src=pr&el=desc) into [master](https://codecov.io/gh/segmentio/terraform-docs/commit/b6241751d92185c40b81bb5a6e1314df25dd0561&el=desc) will **increase** coverage by `0.04%`.
> The diff coverage is `75.00%`.

```diff
@@            Coverage Diff             @@
##           master     #224      +/-   ##
==========================================
+ Coverage   92.45%   92.49%   +0.04%     
==========================================
  Files          20       20              
  Lines         928      933       +5     
==========================================
+ Hits          858      863       +5     
  Misses         48       48              
  Partials       22       22              
```


| [Impacted Files](https://codecov.io/gh/segmentio/terraform-docs/pull/224?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/module/module.go](https://codecov.io/gh/segmentio/terraform-docs/pull/224/diff?src=pr&el=tree#diff-aW50ZXJuYWwvbW9kdWxlL21vZHVsZS5nbw==) | `91.66% <75.00%> (+0.20%)` | :arrow_up: |


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ba82885..1b396ec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,8 @@
 - Add extensive tests coverage for all the packages ([#208](https://github.com/segmentio/terraform-docs/issues/208))
 
 ### Features
+- Allow hiding the "Sensitive" column in markdown ([#223](https://github.com/segmentio/terraform-docs/issues/223))
+- Add section for module requirements ([#222](https://github.com/segmentio/terraform-docs/issues/222))
 - Add support for fetching the module header from any file ([#217](https://github.com/segmentio/terraform-docs/issues/217))
 - Add support for XML renderer ([#198](https://github.com/segmentio/terraform-docs/issues/198))
 - Show sensitivity of the output value in rendered result ([#207](https://github.com/segmentio/terraform-docs/issues/207))
diff --git a/cmd/root.go b/cmd/root.go
index 0f0cb60..b9a5fee 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -25,6 +25,8 @@ var rootCmd = &cobra.Command{
 			return !val
 		}
 		settings.ShowHeader = oppositeBool("no-header")
+		options.ShowHeader = settings.ShowHeader
+
 		settings.ShowInputs = oppositeBool("no-inputs")
 		settings.ShowOutputs = oppositeBool("no-outputs")
 		settings.ShowProviders = oppositeBool("no-providers")
diff --git a/internal/module/module.go b/internal/module/module.go
index b9c3987..d0a7ebf 100644
--- a/internal/module/module.go
+++ b/internal/module/module.go
@@ -39,10 +39,11 @@ func loadModule(path string) (*tfconfig.Module, error) {
 }
 
 func loadModuleItems(tfmodule *tfconfig.Module, options *Options) (*tfconf.Module, error) {
-	header, err := loadHeader(options.Path, options.HeaderFromFile)
+	header, err := loadHeader(options)
 	if err != nil {
 		return nil, err
 	}
+
 	inputs, required, optional := loadInputs(tfmodule)
 	outputs, err := loadOutputs(tfmodule, options)
 	if err != nil {
@@ -63,8 +64,12 @@ func loadModuleItems(tfmodule *tfconfig.Module, options *Options) (*tfconf.Modul
 	}, nil
 }
 
-func loadHeader(base string, path string) (string, error) {
-	filename := filepath.Join(base, path)
+func loadHeader(options *Options) (string, error) {
+	if !options.ShowHeader {
+		return "", nil
+	}
+
+	filename := filepath.Join(options.Path, options.HeaderFromFile)
 	_, err := ioutil.ReadFile(filename)
 	if err != nil {
 		return "", err
diff --git a/internal/module/options.go b/internal/module/options.go
index 546172b..b8c01a3 100644
--- a/internal/module/options.go
+++ b/internal/module/options.go
@@ -17,6 +17,7 @@ type SortBy struct {
 type Options struct {
 	Path             string
 	HeaderFromFile   string
+	ShowHeader       bool
 	SortBy           *SortBy
 	OutputValues     bool
 	OutputValuesPath string
@@ -27,6 +28,7 @@ func NewOptions() *Options {
 	return &Options{
 		Path:             "",
 		HeaderFromFile:   "main.tf",
+		ShowHeader:       true,
 		SortBy:           &SortBy{Name: false, Required: false},
 		OutputValues:     false,
 		OutputValuesPath: "",


Test patch:
diff --git a/internal/format/document_test.go b/internal/format/document_test.go
index 76dd832..0bccbd4 100644
--- a/internal/format/document_test.go
+++ b/internal/format/document_test.go
@@ -494,3 +494,28 @@ func TestDocumentOutputValuesNoSensitivity(t *testing.T) {
 	assert.Nil(err)
 	assert.Equal(expected, actual)
 }
+
+func TestDocumentEmpty(t *testing.T) {
+	assert := assert.New(t)
+	settings := testutil.Settings().With(&print.Settings{
+		ShowHeader:    false,
+		ShowProviders: false,
+		ShowInputs:    false,
+		ShowOutputs:   false,
+	}).Build()
+
+	options, err := module.NewOptions().WithOverwrite(&module.Options{
+		HeaderFromFile: "bad.tf",
+	})
+	options.ShowHeader = false // Since we don't show the header, the file won't be loaded at all
+	assert.Nil(err)
+
+	module, err := testutil.GetModule(options)
+	assert.Nil(err)
+
+	printer := NewDocument(settings)
+	actual, err := printer.Print(module, settings)
+
+	assert.Nil(err)
+	assert.Equal("", actual)
+}
diff --git a/internal/format/json_test.go b/internal/format/json_test.go
index 8139684..1e04cad 100644
--- a/internal/format/json_test.go
+++ b/internal/format/json_test.go
@@ -387,3 +387,31 @@ func TestJsonHeaderFromFile(t *testing.T) {
 	assert.Nil(err)
 	assert.Equal(expected, actual)
 }
+
+func TestJsonEmpty(t *testing.T) {
+	assert := assert.New(t)
+	settings := testutil.Settings().With(&print.Settings{
+		ShowHeader:    false,
+		ShowProviders: false,
+		ShowInputs:    false,
+		ShowOutputs:   false,
+	}).Build()
+
+	expected, err := testutil.GetExpected("json", "json-Empty")
+	assert.Nil(err)
+
+	options, err := module.NewOptions().WithOverwrite(&module.Options{
+		HeaderFromFile: "bad.tf",
+	})
+	options.ShowHeader = false // Since we don't show the header, the file won't be loaded at all
+	assert.Nil(err)
+
+	module, err := testutil.GetModule(options)
+	assert.Nil(err)
+
+	printer := NewJSON(settings)
+	actual, err := printer.Print(module, settings)
+
+	assert.Nil(err)
+	assert.Equal(expected, actual)
+}
diff --git a/internal/format/pretty_test.go b/internal/format/pretty_test.go
index 8ee26f9..db23751 100644
--- a/internal/format/pretty_test.go
+++ b/internal/format/pretty_test.go
@@ -387,3 +387,28 @@ func TestPrettyHeaderFromFile(t *testing.T) {
 	assert.Nil(err)
 	assert.Equal(expected, actual)
 }
+
+func TestPrettyEmpty(t *testing.T) {
+	assert := assert.New(t)
+	settings := testutil.Settings().With(&print.Settings{
+		ShowHeader:    false,
+		ShowProviders: false,
+		ShowInputs:    false,
+		ShowOutputs:   false,
+	}).Build()
+
+	options, err := module.NewOptions().WithOverwrite(&module.Options{
+		HeaderFromFile: "bad.tf",
+	})
+	options.ShowHeader = false // Since we don't show the header, the file won't be loaded at all
+	assert.Nil(err)
+
+	module, err := testutil.GetModule(options)
+	assert.Nil(err)
+
+	printer := NewPretty(settings)
+	actual, err := printer.Print(module, settings)
+
+	assert.Nil(err)
+	assert.Equal("", actual)
+}
diff --git a/internal/format/table_test.go b/internal/format/table_test.go
index 79f6ea7..36f3101 100644
--- a/internal/format/table_test.go
+++ b/internal/format/table_test.go
@@ -494,3 +494,28 @@ func TestTableOutputValuesNoSensitivity(t *testing.T) {
 	assert.Nil(err)
 	assert.Equal(expected, actual)
 }
+
+func TestTableEmpty(t *testing.T) {
+	assert := assert.New(t)
+	settings := testutil.Settings().With(&print.Settings{
+		ShowHeader:    false,
+		ShowProviders: false,
+		ShowInputs:    false,
+		ShowOutputs:   false,
+	}).Build()
+
+	options, err := module.NewOptions().WithOverwrite(&module.Options{
+		HeaderFromFile: "bad.tf",
+	})
+	options.ShowHeader = false // Since we don't show the header, the file won't be loaded at all
+	assert.Nil(err)
+
+	module, err := testutil.GetModule(options)
+	assert.Nil(err)
+
+	printer := NewTable(settings)
+	actual, err := printer.Print(module, settings)
+
+	assert.Nil(err)
+	assert.Equal("", actual)
+}
diff --git a/internal/format/testdata/json/json-Empty.golden b/internal/format/testdata/json/json-Empty.golden
new file mode 100644
index 0000000..e2221b5
--- /dev/null
+++ b/internal/format/testdata/json/json-Empty.golden
@@ -0,0 +1,7 @@
+{
+  "header": "",
+  "inputs": [],
+  "outputs": [],
+  "providers": [],
+  "requirements": []
+}
\ No newline at end of file
diff --git a/internal/format/testdata/xml/xml-Empty.golden b/internal/format/testdata/xml/xml-Empty.golden
new file mode 100644
index 0000000..d6f7a9b
--- /dev/null
+++ b/internal/format/testdata/xml/xml-Empty.golden
@@ -0,0 +1,7 @@
+<module>
+  <header></header>
+  <inputs></inputs>
+  <outputs></outputs>
+  <providers></providers>
+  <requirements></requirements>
+</module>
\ No newline at end of file
diff --git a/internal/format/testdata/yaml/yaml-Empty.golden b/internal/format/testdata/yaml/yaml-Empty.golden
new file mode 100644
index 0000000..c54e04c
--- /dev/null
+++ b/internal/format/testdata/yaml/yaml-Empty.golden
@@ -0,0 +1,5 @@
+header: ""
+inputs: []
+outputs: []
+providers: []
+requirements: []
\ No newline at end of file
diff --git a/internal/format/xml_test.go b/internal/format/xml_test.go
index e411af0..d313ef6 100644
--- a/internal/format/xml_test.go
+++ b/internal/format/xml_test.go
@@ -367,3 +367,31 @@ func TestXmlHeaderFromFile(t *testing.T) {
 	assert.Nil(err)
 	assert.Equal(expected, actual)
 }
+
+func TestXmlEmpty(t *testing.T) {
+	assert := assert.New(t)
+	settings := testutil.Settings().With(&print.Settings{
+		ShowHeader:    false,
+		ShowProviders: false,
+		ShowInputs:    false,
+		ShowOutputs:   false,
+	}).Build()
+
+	expected, err := testutil.GetExpected("xml", "xml-Empty")
+	assert.Nil(err)
+
+	options, err := module.NewOptions().WithOverwrite(&module.Options{
+		HeaderFromFile: "bad.tf",
+	})
+	options.ShowHeader = false // Since we don't show the header, the file won't be loaded at all
+	assert.Nil(err)
+
+	module, err := testutil.GetModule(options)
+	assert.Nil(err)
+
+	printer := NewXML(settings)
+	actual, err := printer.Print(module, settings)
+
+	assert.Nil(err)
+	assert.Equal(expected, actual)
+}
diff --git a/internal/format/yaml_test.go b/internal/format/yaml_test.go
index c3247bd..8ab7fdc 100644
--- a/internal/format/yaml_test.go
+++ b/internal/format/yaml_test.go
@@ -367,3 +367,31 @@ func TestYamlHeaderFromFile(t *testing.T) {
 	assert.Nil(err)
 	assert.Equal(expected, actual)
 }
+
+func TestYamlEmpty(t *testing.T) {
+	assert := assert.New(t)
+	settings := testutil.Settings().With(&print.Settings{
+		ShowHeader:    false,
+		ShowProviders: false,
+		ShowInputs:    false,
+		ShowOutputs:   false,
+	}).Build()
+
+	expected, err := testutil.GetExpected("yaml", "yaml-Empty")
+	assert.Nil(err)
+
+	options, err := module.NewOptions().WithOverwrite(&module.Options{
+		HeaderFromFile: "bad.tf",
+	})
+	options.ShowHeader = false // Since we don't show the header, the file won't be loaded at all
+	assert.Nil(err)
+
+	module, err := testutil.GetModule(options)
+	assert.Nil(err)
+
+	printer := NewYAML(settings)
+	actual, err := printer.Print(module, settings)
+
+	assert.Nil(err)
+	assert.Equal(expected, actual)
+}
diff --git a/internal/module/module_test.go b/internal/module/module_test.go
index c3665bb..7802dad 100644
--- a/internal/module/module_test.go
+++ b/internal/module/module_test.go
@@ -107,7 +107,8 @@ func TestLoadHeader(t *testing.T) {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 			assert := assert.New(t)
-			actual, err := loadHeader(filepath.Join("testdata", tt.path), tt.header)
+			options := &Options{Path: filepath.Join("testdata", tt.path), HeaderFromFile: tt.header, ShowHeader: true}
+			actual, err := loadHeader(options)
 			if tt.wantErr {
 				assert.NotNil(err)
 			} else {