Skip to content

Commit

Permalink
refactor(cli): merge manifest and tool concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeodor committed Jun 12, 2024
1 parent 3f526ff commit 9d074a8
Show file tree
Hide file tree
Showing 30 changed files with 351 additions and 363 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ cli-lint:

cli-test:
@echo "-- Running CLI tests"
cd cli && go test -coverprofile=c.out ./...
cd cli && gotestsum -f testname -- -coverprofile=c.out ./...

cli-dep:
@echo "-- Installing CLI dependencies"
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"

"numerous/cli/cmd/output"
"numerous/cli/internal/dir"
"numerous/cli/internal/gql"
"numerous/cli/internal/gql/app"
"numerous/cli/tool"

"git.sr.ht/~emersion/gqlclient"
"github.com/spf13/cobra"
Expand All @@ -31,7 +31,7 @@ func deleteApp(client *gqlclient.Client, args []string) error {
var appID string
if len(args) == 1 {
appID = args[0]
} else if readAppID, err := tool.ReadAppIDAndPrintErrors("."); err != nil {
} else if readAppID, err := dir.ReadAppIDAndPrintErrors("."); err != nil {
return err
} else {
appID = readAppID
Expand All @@ -41,7 +41,7 @@ func deleteApp(client *gqlclient.Client, args []string) error {
output.PrintError(
"Sorry, we could not find the app in our database.",
"Please, make sure that the App ID in the \"%s\" file is correct and try again.",
tool.AppIDFileName,
dir.AppIDFileName,
)

return err
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package deleteapp
import (
"testing"

"numerous/cli/internal/dir"
"numerous/cli/internal/gql/app"
"numerous/cli/test"
"numerous/cli/tool"

"github.com/stretchr/testify/assert"
)

func TestAppDelete(t *testing.T) {
t.Run("returns nil and successfully sends AppDelete mutations", func(t *testing.T) {
test.ChdirToTmpDirWithAppIDDocument(t, tool.AppIDFileName, "id")
test.ChdirToTmpDirWithAppIDDocument(t, dir.AppIDFileName, "id")
response, _ := test.DeleteSuccessQueryResult()
app := app.App{
ID: "id",
Expand All @@ -26,7 +26,7 @@ func TestAppDelete(t *testing.T) {
transportMock.AssertExpectations(t)
})
t.Run("returns error if app does not exist", func(t *testing.T) {
test.ChdirToTmpDirWithAppIDDocument(t, tool.AppIDFileName, "id")
test.ChdirToTmpDirWithAppIDDocument(t, dir.AppIDFileName, "id")
appNotFoundResponse := `"record not found"`
response, _ := test.DeleteFailureQueryResult(appNotFoundResponse)
c, transportMock := test.CreateMockGqlClient(response)
Expand Down
24 changes: 12 additions & 12 deletions cli/cmd/initialize/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

"numerous/cli/assets"
"numerous/cli/cmd/output"
"numerous/cli/internal/dir"
"numerous/cli/manifest"
"numerous/cli/tool"
)

const EnvFileName string = ".env"

func bootstrapFiles(t tool.Tool, toolID string, basePath string) error {
manifestToml, err := manifest.FromTool(t).ToToml()
func bootstrapFiles(m *manifest.Manifest, toolID string, basePath string) error {
manifestToml, err := m.ToToml()
if err != nil {
output.PrintErrorDetails("Error encoding manifest file", err)

Expand All @@ -26,24 +26,24 @@ func bootstrapFiles(t tool.Tool, toolID string, basePath string) error {
return err
}

if err = addToGitIgnore(basePath, []string{"# added by numerous init\n", tool.AppIDFileName, EnvFileName}); err != nil {
if err = addToGitIgnore(basePath, []string{"# added by numerous init\n", dir.AppIDFileName, EnvFileName}); err != nil {
return err
}

appFilePath := filepath.Join(basePath, t.AppFile)
if err = createAndWriteIfFileNotExist(appFilePath, t.Library.DefaultAppFile()); err != nil {
appFilePath := filepath.Join(basePath, m.AppFile)
if err = createAndWriteIfFileNotExist(appFilePath, m.Library.DefaultAppFile()); err != nil {
return err
}

requirementsFilePath := filepath.Join(basePath, t.RequirementsFile)
requirementsFilePath := filepath.Join(basePath, m.RequirementsFile)
if err = createFile(requirementsFilePath); err != nil {
return err
}

if err := bootstrapRequirements(t, basePath); err != nil {
if err := bootstrapRequirements(m, basePath); err != nil {
return err
}
if err = assets.CopyToolPlaceholderCover(filepath.Join(basePath, t.CoverImage)); err != nil {
if err = assets.CopyToolPlaceholderCover(filepath.Join(basePath, m.CoverImage)); err != nil {
return err
}

Expand All @@ -54,8 +54,8 @@ func bootstrapFiles(t tool.Tool, toolID string, basePath string) error {
return nil
}

func bootstrapRequirements(t tool.Tool, basePath string) error {
requirementsPath := filepath.Join(basePath, t.RequirementsFile)
func bootstrapRequirements(m *manifest.Manifest, basePath string) error {
requirementsPath := filepath.Join(basePath, m.RequirementsFile)
content, err := os.ReadFile(requirementsPath)
if err != nil {
return err
Expand All @@ -67,7 +67,7 @@ func bootstrapRequirements(t tool.Tool, basePath string) error {
}
defer file.Close()

for _, requirement := range t.Library.Requirements {
for _, requirement := range m.Library.Requirements {
if err := addRequirementToFile(file, content, requirement); err != nil {
return err
}
Expand Down
65 changes: 33 additions & 32 deletions cli/cmd/initialize/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"numerous/cli/manifest"
"numerous/cli/test"
"numerous/cli/tool"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -42,9 +41,9 @@ func allFilesExist(fileNames []string) error {
func TestBootstrapAllFiles(t *testing.T) {
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
lib, err := tool.GetLibraryByKey("streamlit")
lib, err := manifest.GetLibraryByKey("streamlit")
require.NoError(t, err)
testTool := tool.Tool{
m := manifest.Manifest{
Library: lib,
AppFile: "app.py",
RequirementsFile: "requirements.txt",
Expand All @@ -53,12 +52,12 @@ func TestBootstrapAllFiles(t *testing.T) {
expectedFiles := []string{
".gitignore",
manifest.ManifestFileName,
testTool.AppFile,
testTool.RequirementsFile,
testTool.CoverImage,
m.AppFile,
m.RequirementsFile,
m.CoverImage,
}

err = bootstrapFiles(testTool, "some-id", tempDir)
err = bootstrapFiles(&m, "some-id", tempDir)

if assert.NoError(t, err) {
err = allFilesExist(expectedFiles)
Expand All @@ -74,49 +73,49 @@ func TestBootstrapRequirementsFile(t *testing.T) {

testCases := []struct {
name string
library tool.Library
library manifest.Library
initialRequirements string
expectedRequirements string
}{
{
name: "plotly-dash without initial requirements",
library: tool.LibraryPlotlyDash,
library: manifest.LibraryPlotlyDash,
initialRequirements: "",
expectedRequirements: "dash\ngunicorn\n",
},
{
name: "streamlit without initial requirements",
library: tool.LibraryStreamlit,
library: manifest.LibraryStreamlit,
initialRequirements: "",
expectedRequirements: "streamlit\n",
},
{
name: "marimo without initial requirements",
library: tool.LibraryMarimo,
library: manifest.LibraryMarimo,
initialRequirements: "",
expectedRequirements: "marimo\n",
},
{
name: "numerous without initial requirements",
library: tool.LibraryNumerous,
library: manifest.LibraryNumerous,
initialRequirements: "",
expectedRequirements: "numerous\n",
},
{
name: "marimo with initial requirements with newline appends at end",
library: tool.LibraryMarimo,
library: manifest.LibraryMarimo,
initialRequirements: dummyRequirementsWithNewLine,
expectedRequirements: dummyRequirementsWithNewLine + "marimo\n",
},
{
name: "marimo with initial requirements without newline appends at end",
library: tool.LibraryMarimo,
library: manifest.LibraryMarimo,
initialRequirements: dummyRequirementsWithoutNewLine,
expectedRequirements: dummyRequirementsWithNewLine + "marimo\n",
},
{
name: "marimo with initial requirements and library is part of requirements, nothing changes",
library: tool.LibraryMarimo,
library: manifest.LibraryMarimo,
initialRequirements: "marimo\n" + dummyRequirementsWithNewLine,
expectedRequirements: "marimo\n" + dummyRequirementsWithNewLine,
},
Expand All @@ -125,21 +124,21 @@ func TestBootstrapRequirementsFile(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
testTool := tool.Tool{
m := manifest.Manifest{
Library: testCase.library,
AppFile: "app.py",
RequirementsFile: "requirements.txt",
CoverImage: "cover_image.png",
}
if testCase.initialRequirements != "" {
err := os.WriteFile(testTool.RequirementsFile, []byte(testCase.initialRequirements), 0o644)
err := os.WriteFile(m.RequirementsFile, []byte(testCase.initialRequirements), 0o644)
require.NoError(t, err)
}

err := bootstrapFiles(testTool, "some-id", tempDir)
err := bootstrapFiles(&m, "some-id", tempDir)

require.NoError(t, err)
actualRequirements, err := os.ReadFile(testTool.RequirementsFile)
actualRequirements, err := os.ReadFile(m.RequirementsFile)
require.NoError(t, err)
assert.Equal(t, testCase.expectedRequirements, string(actualRequirements))
})
Expand Down Expand Up @@ -167,35 +166,35 @@ func TestBootstrapFiles(t *testing.T) {
t.Run("bootstraps app file", func(t *testing.T) {
testCases := []struct {
name string
library tool.Library
library manifest.Library
expectedAppFile string
}{
{
name: "numerous",
library: tool.LibraryNumerous,
library: manifest.LibraryNumerous,
expectedAppFile: expectedNumerousApp,
},
{
name: "streamlit",
library: tool.LibraryStreamlit,
library: manifest.LibraryStreamlit,
expectedAppFile: "",
},
{
name: "dash",
library: tool.LibraryPlotlyDash,
library: manifest.LibraryPlotlyDash,
expectedAppFile: "",
},
{
name: "marimo",
library: tool.LibraryMarimo,
library: manifest.LibraryMarimo,
expectedAppFile: "",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
require.NoError(t, os.Chdir(t.TempDir()))
testTool := tool.Tool{
m := manifest.Manifest{
Library: testCase.library,
AppFile: "app.py",
RequirementsFile: "requirements.txt",
Expand All @@ -204,7 +203,7 @@ func TestBootstrapFiles(t *testing.T) {
tempDir, err := os.Getwd()
require.NoError(t, err)

err = bootstrapFiles(testTool, "tool id", tempDir)
err = bootstrapFiles(&m, "tool id", tempDir)

require.NoError(t, err)
appContent, err := os.ReadFile("app.py")
Expand All @@ -216,7 +215,7 @@ func TestBootstrapFiles(t *testing.T) {
t.Run("adds expected lines to existing .gitignore", func(t *testing.T) {
tmpDir := t.TempDir()
toolID := "tool-id"
tool := tool.Tool{
m := manifest.Manifest{
RequirementsFile: "requirements.txt",
AppFile: "app.py",
CoverImage: "conver_img.png",
Expand All @@ -226,7 +225,7 @@ func TestBootstrapFiles(t *testing.T) {
gitignoreFilePath := filepath.Join(tmpDir, ".gitignore")
test.WriteFile(t, gitignoreFilePath, []byte(initialGitIgnoreContent))

err := bootstrapFiles(tool, toolID, tmpDir)
err := bootstrapFiles(&m, toolID, tmpDir)

assert.NoError(t, err)
actualGitIgnoreContent, err := os.ReadFile(gitignoreFilePath)
Expand All @@ -238,18 +237,20 @@ func TestBootstrapFiles(t *testing.T) {
t.Run("writes manifest with expected excludes", func(t *testing.T) {
tmpDir := t.TempDir()
toolID := "tool-id"
tool := tool.Tool{
m := manifest.Manifest{
RequirementsFile: "requirements.txt",
AppFile: "app.py",
CoverImage: "conver_img.png",
Library: manifest.LibraryMarimo,
Exclude: []string{"*venv", "venv*", ".git", ".env"},
}

bootErr := bootstrapFiles(tool, toolID, tmpDir)
manifest, manifestErr := manifest.LoadManifest(tmpDir + "/" + manifest.ManifestFileName)
bootErr := bootstrapFiles(&m, toolID, tmpDir)
loaded, manifestErr := manifest.LoadManifest(tmpDir + "/" + manifest.ManifestFileName)

assert.NoError(t, bootErr)
assert.NoError(t, manifestErr)
expectedExclude := []string{"*venv", "venv*", ".git", ".env"}
assert.Equal(t, expectedExclude, manifest.Exclude)
assert.Equal(t, expectedExclude, loaded.Exclude)
})
}
4 changes: 2 additions & 2 deletions cli/cmd/initialize/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"numerous/cli/cmd/output"
"numerous/cli/tool"
"numerous/cli/internal/dir"
)

// Creates file if it does not exists
Expand Down Expand Up @@ -81,7 +81,7 @@ func writeOrAppendFile(path string, content string) error {

// Generates and creates file containing the tools id
func createAppIDFile(path string, id string) error {
appIDFile := filepath.Join(path, tool.AppIDFileName)
appIDFile := filepath.Join(path, dir.AppIDFileName)
if err := createFile(appIDFile); err != nil {
output.PrintUnknownError(err)
return err
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/initialize/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"path/filepath"
"testing"

"numerous/cli/internal/dir"
"numerous/cli/test"
"numerous/cli/tool"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -29,7 +29,7 @@ func TestCreateAppIdFile(t *testing.T) {
tmpDir := t.TempDir()
err := os.Chdir(tmpDir)
require.NoError(t, err)
appIDPath := filepath.Join(tmpDir, tool.AppIDFileName)
appIDPath := filepath.Join(tmpDir, dir.AppIDFileName)
appID := "some-id"

err = createAppIDFile(tmpDir, appID)
Expand Down
Loading

0 comments on commit 9d074a8

Please sign in to comment.