Skip to content

Commit

Permalink
Merge pull request #48 from argonui/bundle
Browse files Browse the repository at this point in the history
Loosen restrictions on bundle
  • Loading branch information
argonui authored Nov 22, 2022
2 parents f073f4c + 33290ec commit 60feee4
Show file tree
Hide file tree
Showing 5 changed files with 1,463 additions and 31 deletions.
20 changes: 18 additions & 2 deletions bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ func IsBundled(rawlua string) bool {
return false
}

// AnalyzeBundle exists to help test functions disect bundles
func AnalyzeBundle(rawlua string, log func(s string, a ...interface{})) {
if !IsBundled(rawlua) {
log("script is not bundled\n")
return
}
results, err := UnbundleAll(rawlua)
if err != nil {
log("Couldn't unbundle to analyze: %v", err)
return
}
for k, v := range results {
log("\tmodule %s: %v\n", k, len(v))
}
}

// UnbundleAll takes luacode generates all bundlenames and bundles
func UnbundleAll(rawlua string) (map[string]string, error) {
if !IsBundled(rawlua) {
Expand Down Expand Up @@ -177,12 +193,12 @@ func Bundle(rawlua string, l file.LuaReader) (string, error) {
}

func getAllReqValues(lua string) ([]string, error) {
rsxp := regexp.MustCompile(`(?m)^require\((\\)?\"[a-zA-Z0-9/]*(\\)?\"\)\s*$`)
rsxp := regexp.MustCompile(`require\((\\)?\"[-a-zA-Z0-9/._@]+(\\)?\"\)`)
reqs := rsxp.FindAllString(lua, -1)

fnames := []string{}
for _, req := range reqs {
filexp := regexp.MustCompile(`require\(\\?"([a-zA-Z0-9/]*)\\?"\)`)
filexp := regexp.MustCompile(`require\(\\?"([-a-zA-Z0-9/._@]+)\\?"\)`)
matches := filexp.FindSubmatch([]byte(req))
if len(matches) != 2 {
return nil, fmt.Errorf("regex error parsing requirement (%s)", req)
Expand Down
56 changes: 50 additions & 6 deletions bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ func TestUnbundle3(t *testing.T) {
func TestUnmultiline(t *testing.T) {
raw := `
__bundle_register("__root", function(require, _LOADED, __bundle_register, __bundle_modules)
require("core/AgendaDeck")
require("core/AgendaDeck-other.foo")
var a = '2'
require("core/AgendaDeck")
require("core/AgendaDeck-other.foo")
end)
__bundle_register("core/AgendaDeck", function(require, _LOADED, __bundle_register, __bundle_modules)
__bundle_register("core/AgendaDeck-other.foo", function(require, _LOADED, __bundle_register, __bundle_modules)
var b = '3'
end)
return __bundle_require("__root")
Expand All @@ -258,10 +258,10 @@ return __bundle_require("__root")
t.Fatalf("expected no err, got %v", err)
}
want := map[string]string{
Rootname: `require("core/AgendaDeck")
Rootname: `require("core/AgendaDeck-other.foo")
var a = '2'
require("core/AgendaDeck")`,
"core/AgendaDeck": "var b = '3'",
require("core/AgendaDeck-other.foo")`,
"core/AgendaDeck-other.foo": "var b = '3'",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
Expand Down Expand Up @@ -462,3 +462,47 @@ func TestNonBundled(t *testing.T) {
t.Errorf("want <%s>, got <%s>\n", want, got)
}
}

func TestGetReqs(t *testing.T) {
for _, tc := range []struct {
input, name string
want []string
}{
{
name: "single inputs simple",
input: `local ExtensionHandler = require(\"ExtensionHandler\")\n`,
want: []string{
"ExtensionHandler",
},
},
{
name: "single inputs complicated",
input: `avefile = require(\"campaign-manager.Savefile\")\nloca`,
want: []string{
"campaign-manager.Savefile",
},
},
{
name: "many inputs from GHE",
input: `local ExtensionHandler = require(\"ExtensionHandler\")\n\nlocal Savefile = require(\"campaign-manager.Savefile\")\nlocal Cleanup = require(\"campaign-manager.Cleanup\")\nlocal Achievement = require(\"campaign-manager.Achievement\")\nlocal`,
want: []string{
"ExtensionHandler",
"campaign-manager.Savefile",
"campaign-manager.Cleanup",
"campaign-manager.Achievement",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := getAllReqValues(tc.input)
if err != nil {
t.Fatalf("%v", err)
}

if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
})
}

}
46 changes: 23 additions & 23 deletions compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package main

import (
file "ModCreator/file"
"encoding/json"
"flag"
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

var (
altModfile = flag.String("altmodfile", "", "where to read second mod from when comparing.")
modfilea = flag.String("moda", "", "where to read second mod from when comparing.")
modfileb = flag.String("modb", "", "where to read second mod from when comparing.")
)

func ignoreUnpredictable(k string, v interface{}) bool {
if _, ok := v.(float64); ok {
return true
}
if k == "Date" || k == "EpochTime" {
return true
}

return false
}

func compareDelta(t *testing.T, filea, fileb string) error {
a, err := file.ReadRawFile(filea)
if err != nil {
Expand Down Expand Up @@ -48,15 +60,9 @@ func compareDelta(t *testing.T, filea, fileb string) error {
delete(a, osKey)
delete(b, osKey)

abytes, err := json.Marshal(a)
if err != nil {
return err
if diff := cmp.Diff(a, b, cmpopts.IgnoreMapEntries(ignoreUnpredictable)); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
bbytes, err := json.Marshal(b)
if err != nil {
return err
}
require.JSONEq(t, string(abytes), string(bbytes))
return nil
}

Expand Down Expand Up @@ -148,26 +154,20 @@ func compareObjs(t *testing.T, guid string, a, b map[string]interface{}) error {
return fmt.Errorf("in obj %s, one has sub-objects, the other does not", guid)
}

abytes, err := json.Marshal(a)
if err != nil {
return err
}
bbytes, err := json.Marshal(b)
if err != nil {
return err
if diff := cmp.Diff(a, b, cmpopts.IgnoreMapEntries(ignoreUnpredictable)); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
require.JSONEq(t, string(abytes), string(bbytes))

return nil
}

func TestDiff(t *testing.T) {
if *altModfile == "" || *modfile == "" {
if *modfilea == "" || *modfileb == "" {
// if run automatically, ignore this test
return
}
err := compareDelta(t, *modfile, *altModfile)

err := compareDelta(t, *modfilea, *modfileb)
if err != nil {
t.Errorf("compareDelta(%s,%s) : %v", *modfile, *altModfile, err)
t.Errorf("compareDelta(%s,%s) : %v", *modfilea, *modfileb, err)
}
}
28 changes: 28 additions & 0 deletions tests/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"ModCreator/bundler"
"ModCreator/file"
"ModCreator/mod"
"path/filepath"
Expand Down Expand Up @@ -87,16 +88,43 @@ func TestAllReverseThenBuild(t *testing.T) {
t.Fatalf("output.json not parsed : %v", err)
}
ignoreUnpredictable := func(k string, v interface{}) bool {
if _, ok := v.(float64); ok {
return true
}
if k == "Date" || k == "EpochTime" {
return true
}

return false
}
// bundler.AnalyzeBundle(want["LuaScript"].(string), t.Logf)
// bundler.AnalyzeBundle(got["LuaScript"].(string), t.Logf)
wantBundles, err := bundler.UnbundleAll(want["LuaScript"].(string))
if err != nil {
t.Fatalf("unbundle want : %v", err)
}
gotBundles, err := bundler.UnbundleAll(got["LuaScript"].(string))
if err != nil {
t.Fatalf("unbundle got : %v", err)
}
if diff := cmp.Diff(mapOfKeys(wantBundles), mapOfKeys(gotBundles)); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
delete(want, "LuaScript")
delete(got, "LuaScript")

if diff := cmp.Diff(want, got, cmpopts.IgnoreMapEntries(ignoreUnpredictable)); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
})

}
}

func mapOfKeys(m map[string]string) map[string]interface{} {
r := map[string]interface{}{}
for k := range m {
r[k] = true
}
return r
}
Loading

0 comments on commit 60feee4

Please sign in to comment.