@@ -668,201 +668,6 @@ func TestBuildJavaScript(t *testing.T) {
668668 }
669669}
670670
671- func TestBuildAssemblyScript (t * testing.T ) {
672- if os .Getenv ("TEST_COMPUTE_BUILD_ASSEMBLYSCRIPT" ) == "" && os .Getenv ("TEST_COMPUTE_BUILD" ) == "" {
673- t .Log ("skipping test" )
674- t .Skip ("Set TEST_COMPUTE_BUILD to run this test" )
675- }
676-
677- args := testutil .SplitArgs
678-
679- scenarios := []struct {
680- name string
681- args []string
682- fastlyManifest string
683- wantError string
684- wantRemediationError string
685- wantOutput []string
686- npmInstall bool
687- }{
688- {
689- name : "no fastly.toml manifest" ,
690- args : args ("compute build" ),
691- wantError : "error reading fastly.toml" ,
692- wantRemediationError : "Run `fastly compute init` to ensure a correctly configured manifest." ,
693- },
694- {
695- name : "empty language" ,
696- args : args ("compute build" ),
697- fastlyManifest : `
698- manifest_version = 2
699- name = "test"` ,
700- wantError : "language cannot be empty, please provide a language" ,
701- },
702- {
703- name : "unknown language" ,
704- args : args ("compute build" ),
705- fastlyManifest : `
706- manifest_version = 2
707- name = "test"
708- language = "foobar"` ,
709- wantError : "unsupported language foobar" ,
710- },
711- // The following test validates that the project compiles successfully even
712- // though the fastly.toml manifest has no build script. There should be a
713- // default build script inserted.
714- //
715- // NOTE: This test passes --verbose so we can validate specific outputs.
716- {
717- name : "build script inserted dynamically when missing" ,
718- args : args ("compute build --verbose" ),
719- fastlyManifest : `
720- manifest_version = 2
721- name = "test"
722- language = "assemblyscript"` ,
723- wantOutput : []string {
724- "No [scripts.build] found in fastly.toml." , // requires --verbose
725- "The following default build command for" ,
726- "npm exec -- asc" ,
727- },
728- },
729- {
730- name : "build error" ,
731- args : args ("compute build" ),
732- fastlyManifest : `
733- manifest_version = 2
734- name = "test"
735- language = "assemblyscript"
736-
737- [scripts]
738- build = "echo no compilation happening"` ,
739- wantRemediationError : compute .DefaultBuildErrorRemediation ,
740- },
741- // NOTE: This test passes --verbose so we can validate specific outputs.
742- {
743- name : "successful build" ,
744- args : args ("compute build --verbose" ),
745- fastlyManifest : fmt .Sprintf (`
746- manifest_version = 2
747- name = "test"
748- language = "assemblyscript"
749-
750- [scripts]
751- build = "%s"` , compute .AsDefaultBuildCommand ),
752- wantOutput : []string {
753- "Creating ./bin directory (for Wasm binary)" ,
754- "Built package" ,
755- },
756- npmInstall : true ,
757- },
758- }
759- for testcaseIdx := range scenarios {
760- testcase := & scenarios [testcaseIdx ]
761- t .Run (testcase .name , func (t * testing.T ) {
762- // We're going to chdir to a build environment,
763- // so save the PWD to return to, afterwards.
764- pwd , err := os .Getwd ()
765- if err != nil {
766- t .Fatal (err )
767- }
768-
769- wasmtoolsBinName := "wasm-tools"
770-
771- // Windows was having issues when trying to move a tmpBin file (which
772- // represents the latest binary downloaded from GitHub) to binPath (which
773- // represents the existing binary installed on a user's machine).
774- //
775- // The problem was, for the sake of the tests, I just create one file
776- // `wasmtoolsBinName` and used that for both `tmpBin` and `binPath` and
777- // this works fine on *nix systems. But once Windows did `os.Rename()` and
778- // move tmpBin to binPath it would no longer be able to set permissions on
779- // the binPath because it didn't think the file existed any more. My guess
780- // is that moving a file over itself causes Windows to remove the file.
781- //
782- // So to work around that issue I just create two separate files because
783- // in reality that's what the CLI will be dealing with. I only used one
784- // file for the sake of test case convenience (which ironically became
785- // very INCONVENIENT when the tests started unexpectedly failing on
786- // Windows and caused me a long time debugging).
787- latestDownloaded := wasmtoolsBinName + "-latest-downloaded"
788-
789- // Create test environment
790- rootdir := testutil .NewEnv (testutil.EnvOpts {
791- T : t ,
792- Copy : []testutil.FileIO {
793- {Src : filepath .Join ("testdata" , "build" , "assemblyscript" , "package.json" ), Dst : "package.json" },
794- {Src : filepath .Join ("testdata" , "build" , "assemblyscript" , "assembly" , "index.ts" ), Dst : filepath .Join ("assembly" , "index.ts" )},
795- },
796- Write : []testutil.FileIO {
797- {Src : `#!/usr/bin/env bash
798- echo wasm-tools 1.0.4` , Dst : wasmtoolsBinName , Executable : true },
799- {Src : `#!/usr/bin/env bash
800- echo wasm-tools 2.0.0` , Dst : latestDownloaded , Executable : true },
801- {Src : testcase .fastlyManifest , Dst : manifest .Filename },
802- },
803- })
804- defer os .RemoveAll (rootdir )
805- wasmtoolsBinPath := filepath .Join (rootdir , wasmtoolsBinName )
806-
807- // Before running the test, chdir into the build environment.
808- // When we're done, chdir back to our original location.
809- // This is so we can reliably copy the testdata/ fixtures.
810- if err := os .Chdir (rootdir ); err != nil {
811- t .Fatal (err )
812- }
813- defer func () {
814- _ = os .Chdir (pwd )
815- }()
816-
817- // NOTE: We only want to run `npm install` for the success case.
818- if testcase .npmInstall {
819- // gosec flagged this:
820- // G204 (CWE-78): Subprocess launched with variable
821- // Disabling as we control this command.
822- // #nosec
823- // nosemgrep
824- c := exec .Command ("npm" , "install" )
825- c .Stdout = os .Stdout
826- c .Stderr = os .Stderr
827-
828- err = c .Run ()
829- if err != nil {
830- t .Fatal (err )
831- }
832- }
833-
834- var stdout threadsafe.Buffer
835- app .Init = func (_ []string , _ io.Reader ) (* global.Data , error ) {
836- opts := testutil .MockGlobalData (testcase .args , & stdout )
837- opts .Versioners = global.Versioners {
838- WasmTools : mock.AssetVersioner {
839- AssetVersion : "1.2.3" ,
840- BinaryFilename : wasmtoolsBinName ,
841- DownloadOK : true ,
842- DownloadedFile : latestDownloaded ,
843- InstallFilePath : wasmtoolsBinPath , // avoid overwriting developer's actual wasm-tools install
844- },
845- }
846- return opts , nil
847- }
848- err = app .Run (testcase .args , nil )
849-
850- t .Log (stdout .String ())
851-
852- testutil .AssertRemediationErrorContains (t , err , testcase .wantRemediationError )
853-
854- // NOTE: Some errors we want to assert only the remediation.
855- // e.g. a 'stat' error isn't the same across operating systems/platforms.
856- if testcase .wantError != "" {
857- testutil .AssertErrorContains (t , err , testcase .wantError )
858- }
859- for _ , s := range testcase .wantOutput {
860- testutil .AssertStringContains (t , stdout .String (), s )
861- }
862- })
863- }
864- }
865-
866671// NOTE: TestBuildOther also validates the post_build settings.
867672func TestBuildOther (t * testing.T ) {
868673 args := testutil .SplitArgs
0 commit comments