-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move assets bucket provisioning to separate file
- Loading branch information
1 parent
7059534
commit 7b7f9f3
Showing
4 changed files
with
175 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
const addS3BucketToResources = require("../addS3BucketToResources"); | ||
const parseNextConfiguration = require("../parseNextConfiguration"); | ||
const parsedNextConfigurationFactory = require("../../utils/test/parsedNextConfigurationFactory"); | ||
const ServerlessPluginBuilder = require("../../utils/test/ServerlessPluginBuilder"); | ||
const addAssetsBucketForDeployment = require("../addAssetsBucketForDeployment"); | ||
const logger = require("../../utils/logger"); | ||
|
||
jest.mock("../addS3BucketToResources"); | ||
jest.mock("../parseNextConfiguration"); | ||
jest.mock("../../utils/logger"); | ||
|
||
describe("addAssetsBucketForDeployment", () => { | ||
const mockCFWithBucket = { | ||
Resources: { | ||
NextStaticAssetsBucket: {} | ||
} | ||
}; | ||
|
||
let plugin; | ||
|
||
beforeEach(() => { | ||
plugin = new ServerlessPluginBuilder().build(); | ||
addS3BucketToResources.mockResolvedValue(mockCFWithBucket); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should not call addS3BucketToResources if a staticAssetsBucket is not available", () => { | ||
expect.assertions(1); | ||
parseNextConfiguration.mockReturnValueOnce( | ||
parsedNextConfigurationFactory({}, null) | ||
); | ||
|
||
return addAssetsBucketForDeployment.call(plugin).then(() => { | ||
expect(addS3BucketToResources).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
it("should call parseNextConfiguration with nextConfigDir", () => { | ||
expect.assertions(1); | ||
|
||
const nextConfigDir = "./"; | ||
|
||
plugin = new ServerlessPluginBuilder() | ||
.withNextCustomConfig({ | ||
nextConfigDir | ||
}) | ||
.build(); | ||
|
||
parseNextConfiguration.mockReturnValueOnce( | ||
parsedNextConfigurationFactory() | ||
); | ||
|
||
return addAssetsBucketForDeployment.call(plugin).then(() => { | ||
expect(parseNextConfiguration).toBeCalledWith(nextConfigDir); | ||
}); | ||
}); | ||
|
||
it("should log when a bucket is going to be provisioned from parsed assetPrefix", () => { | ||
expect.assertions(1); | ||
|
||
parseNextConfiguration.mockReturnValueOnce( | ||
parsedNextConfigurationFactory() | ||
); | ||
|
||
return addAssetsBucketForDeployment.call(plugin).then(() => { | ||
expect(logger.log).toBeCalledWith( | ||
expect.stringContaining(`Found bucket "my-bucket"`) | ||
); | ||
}); | ||
}); | ||
|
||
it("should update coreCloudFormationTemplate with static assets bucket", () => { | ||
expect.assertions(2); | ||
|
||
parseNextConfiguration.mockReturnValueOnce( | ||
parsedNextConfigurationFactory() | ||
); | ||
|
||
const initialCoreCF = { | ||
Resources: { bar: "baz" } | ||
}; | ||
|
||
plugin = new ServerlessPluginBuilder() | ||
.withService({ | ||
provider: { | ||
coreCloudFormationTemplate: initialCoreCF | ||
} | ||
}) | ||
.build(); | ||
|
||
return addAssetsBucketForDeployment.call(plugin).then(() => { | ||
const { coreCloudFormationTemplate } = plugin.serverless.service.provider; | ||
|
||
expect(addS3BucketToResources).toBeCalledWith("my-bucket", initialCoreCF); | ||
expect(coreCloudFormationTemplate).toEqual(mockCFWithBucket); | ||
}); | ||
}); | ||
|
||
it("should update compiledCloudFormation with static assets bucket", () => { | ||
expect.assertions(2); | ||
|
||
parseNextConfiguration.mockReturnValueOnce( | ||
parsedNextConfigurationFactory() | ||
); | ||
|
||
const initialCompiledCF = { | ||
Resources: { foo: "bar" } | ||
}; | ||
|
||
const plugin = new ServerlessPluginBuilder() | ||
.withService({ | ||
provider: { | ||
compiledCloudFormationTemplate: initialCompiledCF | ||
} | ||
}) | ||
.build(); | ||
|
||
return addAssetsBucketForDeployment.call(plugin).then(() => { | ||
const { | ||
compiledCloudFormationTemplate | ||
} = plugin.serverless.service.provider; | ||
expect(addS3BucketToResources).toBeCalledWith( | ||
"my-bucket", | ||
initialCompiledCF | ||
); | ||
expect(compiledCloudFormationTemplate).toEqual(mockCFWithBucket); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const parseNextConfiguration = require("./parseNextConfiguration"); | ||
const logger = require("../utils/logger"); | ||
const addS3BucketToResources = require("./addS3BucketToResources"); | ||
|
||
const getCFTemplatesWithBucket = async function(bucketName) { | ||
return Promise.all([ | ||
addS3BucketToResources( | ||
bucketName, | ||
this.serverless.service.provider.compiledCloudFormationTemplate | ||
), | ||
addS3BucketToResources( | ||
bucketName, | ||
this.serverless.service.provider.coreCloudFormationTemplate | ||
) | ||
]); | ||
}; | ||
|
||
module.exports = async function() { | ||
const { staticAssetsBucket } = parseNextConfiguration( | ||
this.getPluginConfigValue("nextConfigDir") | ||
); | ||
|
||
if (!staticAssetsBucket) { | ||
return; | ||
} | ||
|
||
logger.log(`Found bucket "${staticAssetsBucket}" in assetPrefix!`); | ||
|
||
const [ | ||
compiledCfWithBucket, | ||
coreCfWithBucket | ||
] = await getCFTemplatesWithBucket.call(this, staticAssetsBucket); | ||
|
||
this.serverless.service.provider.compiledCloudFormationTemplate = compiledCfWithBucket; | ||
this.serverless.service.provider.coreCloudFormationTemplate = coreCfWithBucket; | ||
|
||
return Promise.resolve(); | ||
}; |