pulumi/tests/testdata/codegen/functions-pp/nodejs/functions.ts

55 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

import * as pulumi from "@pulumi/pulumi";
2022-09-16 23:12:29 +00:00
import * as aws from "@pulumi/aws";
[program-gen] Fix generated utility functions for filebase64, filebase64sha256, sha1 and mimeType (#14857) # Description While writing program tests for generated helper utility functions `filebase64`, `filebase64sha256`, `sha1` and `mimeType` with the idea to increase code coverage, it turned out that those are completely broken in all of the languages containing syntax errors, missing imports and wrong indentation. This PR fixes them and extends the `functions` program to show how they now look like and to show that they compile. Also adding example usage of `stack()`, `project()` and `cwd()` in the test program. ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-15 11:26:00 +00:00
import * as crypto from "crypto";
import * as fs from "fs";
function computeFilebase64sha256(path: string): string {
const fileData = Buffer.from(fs.readFileSync(path, 'binary'))
return crypto.createHash('sha256').update(fileData).digest('hex')
}
function mimeType(path: string): string {
throw new Error("mimeType not implemented, use the mime or mime-types package instead");
}
const encoded = Buffer.from("haha business").toString("base64");
2022-09-16 23:12:29 +00:00
const decoded = Buffer.from(encoded, "base64").toString("utf8");
const joined = [
2022-09-16 23:12:29 +00:00
encoded,
decoded,
"2",
].join("-");
// tests that we initialize "var, err" with ":=" first, then "=" subsequently (Go specific)
const zone = aws.getAvailabilityZones({});
const zone2 = aws.getAvailabilityZones({});
2022-09-16 23:12:29 +00:00
const bucket = new aws.s3.Bucket("bucket", {});
const encoded2 = bucket.id.apply(id => Buffer.from(id).toString("base64"));
const decoded2 = bucket.id.apply(id => Buffer.from(id, "base64").toString("utf8"));
2023-01-31 12:31:00 +00:00
const secretValue = pulumi.secret("hello");
const plainValue = pulumi.unsecret(secretValue);
[program-gen] Fix generated utility functions for filebase64, filebase64sha256, sha1 and mimeType (#14857) # Description While writing program tests for generated helper utility functions `filebase64`, `filebase64sha256`, `sha1` and `mimeType` with the idea to increase code coverage, it turned out that those are completely broken in all of the languages containing syntax errors, missing imports and wrong indentation. This PR fixes them and extends the `functions` program to show how they now look like and to show that they compile. Also adding example usage of `stack()`, `project()` and `cwd()` in the test program. ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-15 11:26:00 +00:00
const currentStack = pulumi.getStack();
const currentProject = pulumi.getProject();
const workingDirectory = process.cwd();
const fileMimeType = mimeType("./base64.txt");
// using the filebase64 function
const first = new aws.s3.BucketObject("first", {
bucket: bucket.id,
source: new pulumi.asset.StringAsset(fs.readFileSync("./base64.txt", { encoding: "base64" })),
[program-gen] Fix generated utility functions for filebase64, filebase64sha256, sha1 and mimeType (#14857) # Description While writing program tests for generated helper utility functions `filebase64`, `filebase64sha256`, `sha1` and `mimeType` with the idea to increase code coverage, it turned out that those are completely broken in all of the languages containing syntax errors, missing imports and wrong indentation. This PR fixes them and extends the `functions` program to show how they now look like and to show that they compile. Also adding example usage of `stack()`, `project()` and `cwd()` in the test program. ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-15 11:26:00 +00:00
contentType: fileMimeType,
tags: {
stack: currentStack,
project: currentProject,
cwd: workingDirectory,
},
});
// using the filebase64sha256 function
const second = new aws.s3.BucketObject("second", {
bucket: bucket.id,
source: new pulumi.asset.StringAsset(computeFilebase64sha256("./base64.txt")),
});
// using the sha1 function
const third = new aws.s3.BucketObject("third", {
bucket: bucket.id,
source: new pulumi.asset.StringAsset(crypto.createHash('sha1').update("content").digest('hex')),
});