Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to match error for a directory that doesn't exist #76

Open
github-actions bot opened this issue Oct 17, 2023 · 0 comments
Open

Update to match error for a directory that doesn't exist #76

github-actions bot opened this issue Oct 17, 2023 · 0 comments
Assignees
Labels

Comments

@github-actions
Copy link
Contributor

https://api.github.com/gogrlx/grlx/blob/7ac33a5c0dc7b3900a3d9ee13c6389ac95a61dad/ingredients/file/fileDirectory_test.go#L121

package file

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/gogrlx/grlx/types"
)

func TestDirectory(t *testing.T) {
	tempDir := t.TempDir()
	sampleDir := filepath.Join(tempDir, "there-is-a-dir-here")
	os.Mkdir(sampleDir, 0o755)
	file := filepath.Join(sampleDir, "there-is-a-file-here")
	os.Create(file)
	fileModeDNE := filepath.Join(sampleDir, "file-mode-does-not-exist")
	tests := []struct {
		name     string
		params   map[string]interface{}
		expected types.Result
		error    error
		test     bool
	}{
		{
			name: "IncorrectFilename",
			params: map[string]interface{}{
				"name": 1,
			},
			expected: types.Result{
				Succeeded: false,
				Failed:    true,
				Notes:     []fmt.Stringer{},
			},
			error: types.ErrMissingName,
		},
		{
			name: "DirectoryRoot",
			params: map[string]interface{}{
				"name": "/",
			},
			expected: types.Result{
				Succeeded: false,
				Failed:    true,
				Notes:     []fmt.Stringer{},
			},
			error: types.ErrDeleteRoot,
		},
		{
			name: "DirectoryExistingNoAction",
			params: map[string]interface{}{
				"name": sampleDir,
			},
			expected: types.Result{
				Succeeded: false,
				Failed:    true,
				Notes:     []fmt.Stringer{types.Snprintf("creating directory %s", sampleDir)},
			},
			error: nil,
		},
		{
			name: "DirectoryChangeMode",
			params: map[string]interface{}{
				"name":     sampleDir,
				"dir_mode": "755",
			},
			expected: types.Result{
				Succeeded: true,
				Failed:    false,
				Notes:     []fmt.Stringer{types.Snprintf("creating directory %s", sampleDir), types.Snprintf("chmod %s to 755", sampleDir)},
			},
			error: nil,
		},
		{
			name: "DirectoryTestChangeDirMode",
			params: map[string]interface{}{
				"name":     sampleDir,
				"dir_mode": "755",
				"makedirs": true,
			},
			expected: types.Result{
				Succeeded: true,
				Failed:    false,
				Notes:     []fmt.Stringer{types.Snprintf("would create directory %s", sampleDir), types.Snprintf("would chmod %s to 755", sampleDir)},
			},
			test:  true,
			error: nil,
		},
		{
			name: "DirectoryChangeModeNotExist",
			params: map[string]interface{}{
				"name":     fileModeDNE,
				"dir_mode": "755",
				"makedirs": false,
			},
			expected: types.Result{
				Succeeded: false,
				Failed:    true,
				Notes:     []fmt.Stringer{},
			},
			error: fmt.Errorf("chmod %s: no such file or directory", fileModeDNE),
		},
		{
			name: "DirectoryTestChageFileMode",
			params: map[string]interface{}{
				"name":      sampleDir,
				"file_mode": "755",
				"makedirs":  true,
			},
			expected: types.Result{
				Succeeded: true,
				Failed:    false,
				Notes:     []fmt.Stringer{types.Snprintf("would create directory %s", sampleDir), types.Snprintf("would chmod %s to 755", sampleDir)},
			},
			test:  true,
			error: nil,
		},
		{
			// TODO: Update to match error for a directory that doesn't exist
			name: "DirectoryChangeFileModeNotExist",
			params: map[string]interface{}{
				"name":      fileModeDNE,
				"file_mode": "755",
				"makedirs":  false,
			},
			expected: types.Result{
				Succeeded: false,
				Failed:    true,
				Notes:     []fmt.Stringer{},
			},
			error: fmt.Errorf("chmod %s: no such file or directory", fileModeDNE),
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			f := File{
				id:     "",
				method: "",
				params: test.params,
			}
			result, err := f.directory(context.Background(), test.test)
			if err != nil || test.error != nil {
				if (err == nil && test.error != nil) || (err != nil && test.error == nil) {
					t.Errorf("expected error `%v`, got `%v`", test.error, err)
				} else if err.Error() != test.error.Error() {
					t.Errorf("expected error %v, got %v", test.error, err)
				}
			}
			compareResults(t, result, test.expected)
		})
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant