Skip to content

Commit

Permalink
test: refactor tests for remote sources and add some more
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Aug 7, 2019
1 parent 060075f commit a186f60
Show file tree
Hide file tree
Showing 40 changed files with 409 additions and 303 deletions.
3 changes: 2 additions & 1 deletion garden-service/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class GitHandler extends VcsHandler {
* We need to exclude .garden to avoid errors when path is the project root. This happens e.g. for modules
* whose config is colocated with the project config, and that don't specify include paths/patterns.
*/
// FIXME: We should use `garden.gardenDirPath` instead of ".garden" since the dir name can be variable
// FIXME: We should use `garden.gardenDirPath` instead of ".garden" since the gardenDirPath
// property is configurable.
lines = await git("ls-files", "-s", "--others", "--exclude=.garden", path)

// List ignored files from .gardenignore. We need to run ls-files twice to get both tracked and untracked files.
Expand Down
4 changes: 2 additions & 2 deletions garden-service/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import { isConfigFilename } from "./util/fs"
let watcher: FSWatcher | undefined

// Export so that we can clean up the global watcher instance when running tests
export function cleanUp() {
export function cleanUpGlobalWatcher() {
if (watcher) {
watcher.close()
watcher = undefined
}
}

// The process hangs after tests if we don't do this
registerCleanupFunction("stop watcher", cleanUp)
registerCleanupFunction("stop watcher", cleanUpGlobalWatcher)

export type ChangeHandler = (module: Module | null, configChanged: boolean) => Promise<void>

Expand Down
79 changes: 63 additions & 16 deletions garden-service/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
*/

import * as td from "testdouble"
import Bluebird = require("bluebird")
import { resolve, join } from "path"
import { extend } from "lodash"
import { remove, readdirSync, existsSync } from "fs-extra"
import { remove, readdirSync, existsSync, copy, mkdirp, pathExists, truncate } from "fs-extra"
import execa = require("execa")

import { containerModuleSpecSchema, containerTestSchema, containerTaskSchema } from "../src/plugins/container/config"
import { testExecModule, buildExecModule, execBuildSpecSchema } from "../src/plugins/exec"
import { TaskResults } from "../src/task-graph"
Expand All @@ -25,7 +28,7 @@ import { Garden, GardenParams } from "../src/garden"
import { ModuleConfig } from "../src/config/module"
import { mapValues, fromPairs } from "lodash"
import { ModuleVersion } from "../src/vcs/vcs"
import { GARDEN_SERVICE_ROOT } from "../src/constants"
import { GARDEN_SERVICE_ROOT, LOCAL_CONFIG_FILENAME } from "../src/constants"
import { EventBus, Events } from "../src/events"
import { ValueOf } from "../src/util/util"
import { LogEntry } from "../src/logger/log-entry"
Expand All @@ -39,6 +42,7 @@ import { DeleteSecretParams } from "../src/types/plugin/provider/deleteSecret"
import { RunServiceParams } from "../src/types/plugin/service/runService"
import { RunTaskParams, RunTaskResult } from "../src/types/plugin/task/runTask"
import { RunResult } from "../src/types/plugin/base"
import { ExternalSourceType, getRemoteSourceRelPath, hashRepoUrl } from "../src/util/ext-source-util"

export const dataDir = resolve(GARDEN_SERVICE_ROOT, "test", "unit", "data")
export const examplesDir = resolve(GARDEN_SERVICE_ROOT, "..", "examples")
Expand All @@ -50,6 +54,10 @@ export const testModuleVersion: ModuleVersion = {
files: [],
}

// All test projects use this git URL
export const testGitUrl = "https://my-git-server.com/my-repo.git#master"
export const testGitUrlHash = hashRepoUrl(testGitUrl)

export function getDataDir(...names: string[]) {
return resolve(dataDir, ...names)
}
Expand Down Expand Up @@ -379,20 +387,6 @@ export const cleanProject = async (gardenDirPath: string) => {
return remove(gardenDirPath)
}

/**
* Prevents git cloning. Use if creating a Garden instance with test-project-ext-module-sources
* or test-project-ext-project-sources as project root.
*/
export function stubExtSources(garden: Garden) {
td.replace(garden.vcs, "cloneRemoteSource", async () => undefined)
td.replace(garden.vcs, "updateRemoteSource", async () => undefined)

const getRemoteSourcesDirname = td.replace(garden.vcs, "getRemoteSourcesDirname")

td.when(getRemoteSourcesDirname("module")).thenReturn(join("sources", "module"))
td.when(getRemoteSourcesDirname("project")).thenReturn(join("sources", "project"))
}

export function getExampleProjects() {
const names = readdirSync(examplesDir).filter(n => {
const basePath = join(examplesDir, n)
Expand All @@ -416,3 +410,56 @@ export function freezeTime(date?: Date) {
timekeeper.freeze(date)
return date
}

export async function resetLocalConfig(gardenDirPath: string) {
const path = join(gardenDirPath, LOCAL_CONFIG_FILENAME)
if (await pathExists(path)) {
await truncate(path)
}
}

/**
* Idempotently initializes the test-project-ext-project-sources project and returns
* the Garden class.
*/
export async function makeExtProjectSourcesGarden() {
const projectRoot = resolve(dataDir, "test-project-ext-project-sources")
// Borrow the external sources from here:
const extSourcesRoot = resolve(dataDir, "test-project-local-project-sources")
const sourceNames = ["source-a", "source-b", "source-c"]
return prepareRemoteGarden({ projectRoot, extSourcesRoot, sourceNames, type: "project" })
}

/**
* Idempotently initializes the test-project-ext-project-sources project and returns
* the Garden class.
*/
export async function makeExtModuleSourcesGarden() {
const projectRoot = resolve(dataDir, "test-project-ext-module-sources")
// Borrow the external sources from here:
const extSourcesRoot = resolve(dataDir, "test-project-local-module-sources")
const sourceNames = ["module-a", "module-b", "module-c"]
return prepareRemoteGarden({ projectRoot, extSourcesRoot, sourceNames, type: "module" })
}

/**
* Helper function for idempotently initializing the ext-sources projects.
* Copies the external sources into the .garden directory and git inits them.
*/
async function prepareRemoteGarden({
projectRoot, extSourcesRoot, sourceNames, type,
}: { projectRoot: string, extSourcesRoot: string, sourceNames: string[], type: ExternalSourceType }) {
const garden = await makeTestGarden(projectRoot)
const sourcesPath = join(projectRoot, ".garden", "sources", type)

await mkdirp(sourcesPath)
// Copy the sources to the `.garden/sources` dir and git init them
await Bluebird.map(sourceNames, async (name) => {
const remoteSourceRelPath = getRemoteSourceRelPath({ name, url: testGitUrl, sourceType: type })
const targetPath = join(projectRoot, ".garden", remoteSourceRelPath)
await copy(join(extSourcesRoot, name), targetPath)
await execa("git", ["init"], { cwd: targetPath })
})

return garden
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
project:
name: test-project-ext-project-sources
environmentDefaults:
variables:
some: variable
environments:
- name: local
providers:
- name: test-plugin
- name: test-plugin-b
- name: other

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module:
name: module-a
repositoryUrl: https://my-git-server.com/module-a.git#master
repositoryUrl: https://my-git-server.com/my-repo.git#master
type: test
services:
- name: service-a
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module:
name: module-b
repositoryUrl: https://my-git-server.com/module-b.git#master
repositoryUrl: https://my-git-server.com/my-repo.git#master
type: test
services:
- name: service-b
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module:
name: module-c
repositoryUrl: https://my-git-server.com/module-c.git#master
repositoryUrl: https://my-git-server.com/my-repo.git#master
type: test
services:
- name: service-c
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ project:
name: test-project-ext-project-sources
sources:
- name: source-a
repositoryUrl: https://my-git-server.com/source-a.git#master
repositoryUrl: https://my-git-server.com/my-repo.git#master
- name: source-b
repositoryUrl: https://my-git-server.com/source-b.git#master
repositoryUrl: https://my-git-server.com/my-repo.git#master
- name: source-c
repositoryUrl: https://my-git-server.com/source-c.git#master
repositoryUrl: https://my-git-server.com/my-repo.git#master
environmentDefaults:
variables:
some: variable
Expand Down
Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This is a dummy local version of a remote module source. That is, a remote module that a user has a copy of on their local machine and wants to link to.

Used by the `test-project-ext-module-sources` project for linking its modules to a local path. Equivalent to the following:

```sh
# In test-project-ext-module-sources dir
garden link module module-a ../test-project-local-module-sources/module-a
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This is a dummy local version of a remote project source. That is, a remote source that a user has a copy of on their local machine and wants to link to.

Used by the `test-project-ext-project-sources` project for linking its sources to a local path. Equivalent to the following:

```sh
# In test-project-ext-project-sources dir
garden link source source-a ../test-project-local-project-sources/source-a
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
Loading

0 comments on commit a186f60

Please sign in to comment.