github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/analyzers/nodejs/integration_test.go (about)

     1  package nodejs_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/apex/log"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/fossas/fossa-cli/analyzers"
    12  	"github.com/fossas/fossa-cli/exec"
    13  	"github.com/fossas/fossa-cli/files"
    14  	"github.com/fossas/fossa-cli/module"
    15  	"github.com/fossas/fossa-cli/pkg"
    16  	"github.com/fossas/fossa-cli/testing/fixtures"
    17  	"github.com/fossas/fossa-cli/testing/runfossa"
    18  )
    19  
    20  var nodeAnalyzerFixtureDir = filepath.Join(fixtures.Directory(), "nodejs", "analyzer")
    21  
    22  func TestNodejsIntegration(t *testing.T) {
    23  	if testing.Short() {
    24  		t.Skip("Integration tests to not run with the -short test flag")
    25  	}
    26  	t.Parallel()
    27  
    28  	fixtures.Initialize(nodeAnalyzerFixtureDir, projects, projectInitializer)
    29  
    30  	for _, project := range projects {
    31  		proj := project
    32  		t.Run("Analysis: "+proj.Name, func(t *testing.T) {
    33  			t.Parallel()
    34  
    35  			module := module.Module{
    36  				Dir:         filepath.Join(nodeAnalyzerFixtureDir, proj.Name),
    37  				Type:        pkg.NodeJS,
    38  				Name:        proj.Name,
    39  				Options:     proj.ModuleOptions,
    40  				BuildTarget: filepath.Join(nodeAnalyzerFixtureDir, proj.Name),
    41  			}
    42  
    43  			analyzer, err := analyzers.New(module)
    44  			assert.NoError(t, err)
    45  
    46  			deps, err := analyzer.Analyze()
    47  			assert.NoError(t, err)
    48  			// faker has no deps
    49  			if proj.Name == "fakerjs" {
    50  				assert.Empty(t, deps.Direct)
    51  				assert.Empty(t, deps.Transitive)
    52  			} else {
    53  				assert.NotEmpty(t, deps.Direct)
    54  				assert.NotEmpty(t, deps.Transitive)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func projectInitializer(proj fixtures.Project, projectDir string) error {
    61  	nodeModulesExist, err := files.ExistsFolder(projectDir, "node_modules")
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if nodeModulesExist {
    67  		log.Debug("node_modules already exists for " + proj.Name + "skipping initialization")
    68  		return nil
    69  	}
    70  
    71  	_, errOut, err := exec.Run(exec.Cmd{
    72  		Name:    "npm",
    73  		Argv:    []string{"install", "--production"},
    74  		Dir:     projectDir,
    75  		WithEnv: proj.Env,
    76  		Command: "npm",
    77  	})
    78  	if err != nil {
    79  		log.Error(errOut)
    80  		log.Error("failed to run npm install on " + proj.Name)
    81  		return err
    82  	}
    83  
    84  	ymlAlreadyExists, err := files.Exists(filepath.Join(projectDir, ".fossa.yml"))
    85  	if err != nil {
    86  		return err
    87  	}
    88  	if ymlAlreadyExists {
    89  		return nil
    90  	}
    91  
    92  	stdout, stderr, err := runfossa.Init(projectDir)
    93  	if err != nil {
    94  		log.Error("failed to run fossa init on " + proj.Name)
    95  		log.Error(stdout)
    96  		log.Error(stderr)
    97  		return err
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  var projects = []fixtures.Project{
   104  	fixtures.Project{
   105  		Name:   "fakerjs",
   106  		URL:    "https://github.com/Marak/faker.js",
   107  		Commit: "3a4bb358614c1e1f5d73f4df45c13a1a7aa013d7",
   108  	},
   109  	fixtures.Project{
   110  		Name:   "nest",
   111  		URL:    "https://github.com/nestjs/nest",
   112  		Commit: "ce498e86150f7de4a260f0c393d47ec4cc920ea1",
   113  	},
   114  	fixtures.Project{
   115  
   116  		Name:   "express",
   117  		URL:    "https://github.com/expressjs/express",
   118  		Commit: "b4eb1f59d39d801d7365c86b04500f16faeb0b1c",
   119  	},
   120  	fixtures.Project{
   121  		Name:          "standard",
   122  		URL:           "https://github.com/standard/standard",
   123  		Commit:        "bc02256fa2c03632e657248483c55a752e63e724",
   124  		ModuleOptions: map[string]interface{}{"allow-npm-err": true},
   125  	},
   126  }
   127  
   128  func cleanUp(dir string) {
   129  	err := os.RemoveAll(dir)
   130  	if err != nil {
   131  		panic(err)
   132  	}
   133  }