Skip to content

Commit

Permalink
feat: update documentation and add helper/ tests
Browse files Browse the repository at this point in the history
To be added:
note tests
  • Loading branch information
anirudhsudhir committed Apr 28, 2024
1 parent 791fa95 commit 3ee4d49
Show file tree
Hide file tree
Showing 21 changed files with 484 additions and 313 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ anna
*.exe
**/.DS_Store
*.prof
ssg/
*.txt
dist/

#Test directories
**/rendered/
test/**/static/
test/**/got_sitemap.xml
test/**/got_sitemap.xml
3 changes: 3 additions & 0 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (cmd *Cmd) VanillaRender() {
// Copies the contents of the 'static/' directory to 'rendered/'
helper.CopyDirectoryContents(helpers.SiteDataPath+"static/", helpers.SiteDataPath+"rendered/static/")

// Copies the contents of the 'static/' directory to 'rendered/'
helper.CopyDirectoryContents(helpers.SiteDataPath+"public/", helpers.SiteDataPath+"rendered/")

e.GenerateSitemap(helpers.SiteDataPath + "rendered/sitemap.xml")
e.GenerateFeed()
e.GenerateJSONIndex(helpers.SiteDataPath)
Expand Down
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.2

require (
github.com/PuerkitoBio/goquery v1.9.1
github.com/cheggaaa/pb/v3 v3.1.5
github.com/mangoumbrella/goldmark-figure v1.0.0
github.com/spf13/cobra v1.8.0
github.com/yuin/goldmark v1.7.1
Expand All @@ -15,18 +14,11 @@ require (
)

require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/cheggaaa/pb/v3 v3.1.5 h1:QuuUzeM2WsAqG2gMqtzaWithDJv0i+i6UlnwSCI4QLk=
github.com/cheggaaa/pb/v3 v3.1.5/go.mod h1:CrxkeghYTXi1lQBEI7jSn+3svI3cuc19haAj6jM60XI=
github.com/chromedp/cdproto v0.0.0-20230220211738-2b1ec77315c9 h1:wMSvdj3BswqfQOXp2R1bJOAE7xIQLt2dlMQDMf836VY=
github.com/chromedp/cdproto v0.0.0-20230220211738-2b1ec77315c9/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/chromedp v0.9.1 h1:CC7cC5p1BeLiiS2gfNNPwp3OaUxtRMBjfiw3E3k6dFA=
Expand All @@ -16,8 +12,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
Expand All @@ -39,19 +33,9 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mangoumbrella/goldmark-figure v1.0.0 h1:L+ebP73dET0a2n68PV7m5oFJmwwH5GzRIJLgszR3fwo=
github.com/mangoumbrella/goldmark-figure v1.0.0/go.mod h1:iIL+fhdmCQDpE0l/TKtGhokWzIbo5lo/Y2OIAcx6usI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
Expand Down Expand Up @@ -90,9 +74,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/anna_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (e *Engine) GenerateJSONIndex(outFilePath string) {
// It extracts data from the e.Templates slice
// The index.json file is created during every VanillaRender()

jsonFile, err := os.Create(outFilePath + "/rendered/static/index.json")
jsonFile, err := os.Create(outFilePath + "rendered/static/index.json")
if err != nil {
e.ErrorLogger.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/anna_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestGenerateMergedJson(t *testing.T) {
},
}

e.GenerateJSONIndex(TestDirPath + "json_index_test")
e.GenerateJSONIndex(TestDirPath + "json_index_test/")

got_json, err := os.ReadFile(TestDirPath + "/json_index_test/rendered/static/index.json")
if err != nil {
Expand Down
182 changes: 88 additions & 94 deletions pkg/helpers/helper.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package helpers

import (
"archive/zip"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/cheggaaa/pb/v3"
)

const SiteDataPath string = "site/"
Expand Down Expand Up @@ -87,91 +81,91 @@ func (h *Helper) CreateRenderedDir(fileOutPath string) {
}
}

func (h *Helper) Bootstrap() {
fmt.Println("Are you sure you want to proceed with the bootstrap process? (y/n)")
var confirm string
fmt.Scanln(&confirm)
if confirm != "y" {
fmt.Println("Bootstrap process cancelled.")
return
}
url := fmt.Sprintf("https://github.com/acmpesuecc/anna/archive/refs/tags/v%s.zip", version)

output, err := os.Create("anna-dl.zip")
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error downloading:", err)
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
fmt.Println("Error: Server returned status", response.Status)
return
}

bar := pb.Full.Start64(response.ContentLength)
defer bar.Finish()
reader := bar.NewProxyReader(response.Body)

_, err = io.Copy(output, reader)
if err != nil {
fmt.Println("Error copying:", err)
return
}
ext(version)
}

func ext(version string) {
zipFilePath := "anna-dl.zip"
zipFile, err := zip.OpenReader(zipFilePath)
if err != nil {
log.Fatal(err)
}
defer zipFile.Close()
for _, file := range zipFile.File {
zippedFile, err := file.Open()
if err != nil {
log.Fatal(err)
}
defer zippedFile.Close()

extractedFilePath := filepath.Join(".", file.Name) // Extract to the current directory
if file.FileInfo().IsDir() {
err := os.MkdirAll(extractedFilePath, os.ModePerm)
if err != nil {
log.Fatal(err)
}
} else {
extractedFile, err := os.Create(extractedFilePath)
if err != nil {
log.Fatal(err)
}
defer extractedFile.Close()

_, err = io.Copy(extractedFile, zippedFile)
if err != nil {
log.Fatal(err)
}
}
}

// Bootstrap the base theme
helper := &Helper{
ErrorLogger: log.New(os.Stderr, "ERROR: ", log.LstdFlags),
}
helper.CopyDirectoryContents(fmt.Sprintf("anna-%s/site/", version), "site/")
err = os.Remove("anna-dl.zip")
if err != nil {
log.Fatal("Error cleaning-up zip file:", err)
}
err = os.RemoveAll(fmt.Sprintf("anna-%s", version))
if err != nil {
log.Fatal("Error deleting directory:", err)
}
log.Println("Bootstrapped base theme")
}
// func (h *Helper) Bootstrap() {
// fmt.Println("Are you sure you want to proceed with the bootstrap process? (y/n)")
// var confirm string
// fmt.Scanln(&confirm)
// if confirm != "y" {
// fmt.Println("Bootstrap process cancelled.")
// return
// }
// url := fmt.Sprintf("https://github.com/acmpesuecc/anna/archive/refs/tags/v%s.zip", version)

// output, err := os.Create("anna-dl.zip")
// if err != nil {
// fmt.Println("Error creating output file:", err)
// return
// }
// defer output.Close()
// response, err := http.Get(url)
// if err != nil {
// fmt.Println("Error downloading:", err)
// return
// }
// defer response.Body.Close()
// if response.StatusCode != http.StatusOK {
// fmt.Println("Error: Server returned status", response.Status)
// return
// }

// bar := pb.Full.Start64(response.ContentLength)
// defer bar.Finish()
// reader := bar.NewProxyReader(response.Body)

// _, err = io.Copy(output, reader)
// if err != nil {
// fmt.Println("Error copying:", err)
// return
// }
// ext(version)
// }

// func ext(version string) {
// zipFilePath := "anna-dl.zip"
// zipFile, err := zip.OpenReader(zipFilePath)
// if err != nil {
// log.Fatal(err)
// }
// defer zipFile.Close()
// for _, file := range zipFile.File {
// zippedFile, err := file.Open()
// if err != nil {
// log.Fatal(err)
// }
// defer zippedFile.Close()

// extractedFilePath := filepath.Join(".", file.Name) // Extract to the current directory
// if file.FileInfo().IsDir() {
// err := os.MkdirAll(extractedFilePath, os.ModePerm)
// if err != nil {
// log.Fatal(err)
// }
// } else {
// extractedFile, err := os.Create(extractedFilePath)
// if err != nil {
// log.Fatal(err)
// }
// defer extractedFile.Close()

// _, err = io.Copy(extractedFile, zippedFile)
// if err != nil {
// log.Fatal(err)
// }
// }
// }

// // Bootstrap the base theme
// helper := &Helper{
// ErrorLogger: log.New(os.Stderr, "ERROR: ", log.LstdFlags),
// }
// helper.CopyDirectoryContents(fmt.Sprintf("anna-%s/site/", version), "site/")
// err = os.Remove("anna-dl.zip")
// if err != nil {
// log.Fatal("Error cleaning-up zip file:", err)
// }
// err = os.RemoveAll(fmt.Sprintf("anna-%s", version))
// if err != nil {
// log.Fatal("Error deleting directory:", err)
// }
// log.Println("Bootstrapped base theme")
// }
68 changes: 56 additions & 12 deletions pkg/helpers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,69 @@ package helpers_test
import (
"io/fs"
"log"
"os"
"slices"
"testing"
"testing/fstest"

"github.com/acmpesuecc/anna/pkg/helpers"
)

type Helper struct {
ErrorLogger *log.Logger
SiteDataPath string
}
var HelperTestDirPath = "../../test/helpers/"

func TestCopyDirectoryContents(t *testing.T) {
t.Run("Testinf copying contents from A to B", func(t *testing.T) {
fsMock := fstest.MapFS{
"index.md": {Data: []byte("# Index Page")},
"about.md": {Data: []byte("# About Page")},
t.Run("recursively copying directory contents", func(t *testing.T) {
helper := helpers.Helper{
ErrorLogger: log.New(os.Stderr, "TEST ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
helper.CopyDirectoryContents(HelperTestDirPath+"copy_dir/", HelperTestDirPath+"copy_dir/rendered/")

baseDirFS := os.DirFS(HelperTestDirPath + "copy_dir/input_dir/")
TraverseDirectory(baseDirFS, t)
})
}

func TraverseDirectory(baseDirFS fs.FS, t *testing.T) error {
fs.WalkDir(baseDirFS, ".", func(path string, dir fs.DirEntry, err error) error {
if !dir.IsDir() {
got_file, err := os.ReadFile(HelperTestDirPath + "copy_dir/input_dir/" + path)
if err != nil {
t.Errorf("%v", err)
}

want_file, err := os.ReadFile(HelperTestDirPath + "copy_dir/rendered/input_dir/" + path)
if err != nil {
t.Errorf("%v", err)
}

if !slices.Equal(got_file, want_file) {
t.Errorf("The expected and generated files can be found in %s", HelperTestDirPath)
}

}
return nil
})
return nil
}

fs.WalkDir(fsMock, ".", func(path string, d fs.DirEntry, err error) error {
return nil
})
func TestCopyFiles(t *testing.T) {
t.Run("copy file and create nested parent directories", func(t *testing.T) {
helper := helpers.Helper{
ErrorLogger: log.New(os.Stderr, "TEST ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
helper.CopyFiles(HelperTestDirPath+"copy_files/input.txt", HelperTestDirPath+"copy_files/rendered/output.txt")

got_file, err := os.ReadFile(HelperTestDirPath + "copy_files/input.txt")
if err != nil {
t.Errorf("%v", err)
}

want_file, err := os.ReadFile(HelperTestDirPath + "copy_files/rendered/output.txt")
if err != nil {
t.Errorf("%v", err)
}

if !slices.Equal(got_file, want_file) {
t.Errorf("The expected and generated files can be found in %s", HelperTestDirPath)
}
})
}
Loading

0 comments on commit 3ee4d49

Please sign in to comment.