Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete env var after test if it was not set #774

Merged
merged 5 commits into from
Jan 4, 2020

Conversation

olavloite
Copy link
Contributor

Delete the environment variable SPANNER_EMULATOR_HOST after a test instead of setting it to undefined when it was not defined in the first place. Setting the environment variable to undefined will convert it to the string 'undefined' and could affect other test cases, as they would see the environment variable as having been set.

@olavloite olavloite requested a review from hengfengli December 28, 2019 07:58
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Dec 28, 2019
@olavloite olavloite force-pushed the delete-env-var-after-test branch from c84b438 to ba69cd0 Compare December 28, 2019 07:59
@codecov
Copy link

codecov bot commented Dec 28, 2019

Codecov Report

Merging #774 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #774   +/-   ##
=======================================
  Coverage   74.58%   74.58%           
=======================================
  Files          43       43           
  Lines       20220    20220           
  Branches      546      546           
=======================================
  Hits        15082    15082           
  Misses       5133     5133           
  Partials        5        5

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update de67b75...4317924. Read the comment docs.

@AVaksman
Copy link
Contributor

LGTM!

proposing wrap these 5 tests in a describe and utilize beforeEach() and afterEach() to handle SPANNER_EMULATOR_HOST env var, then we can omit try ... finally block in each test.

    describe('SPANNER_EMULATOR_HOST', () => {
      let currentEmulator: string | undefined;

      beforeEach(() => (currentEmulator = process.env.SPANNER_EMULATOR_HOST));

      afterEach(() => {
        if (currentEmulator) {
          process.env.SPANNER_EMULATOR_HOST = currentEmulator;
        } else {
          delete process.env.SPANNER_EMULATOR_HOST;
        }
      });

      it('should parse emulator host without port correctly', () => {
        const EMULATOR_HOST = 'somehost.local';
        process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;

        const emulator = Spanner.getSpannerEmulatorHost();

        assert.deepStrictEqual(emulator, {endpoint: EMULATOR_HOST});
      });
    });

@olavloite
Copy link
Contributor Author

LGTM!

proposing wrap these 5 tests in a describe and utilize beforeEach() and afterEach() to handle SPANNER_EMULATOR_HOST env var, then we can omit try ... finally block in each test.

    describe('SPANNER_EMULATOR_HOST', () => {
      let currentEmulator: string | undefined;

      beforeEach(() => (currentEmulator = process.env.SPANNER_EMULATOR_HOST));

      afterEach(() => {
        if (currentEmulator) {
          process.env.SPANNER_EMULATOR_HOST = currentEmulator;
        } else {
          delete process.env.SPANNER_EMULATOR_HOST;
        }
      });

      it('should parse emulator host without port correctly', () => {
        const EMULATOR_HOST = 'somehost.local';
        process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;

        const emulator = Spanner.getSpannerEmulatorHost();

        assert.deepStrictEqual(emulator, {endpoint: EMULATOR_HOST});
      });
    });

Done.
Thanks for the good tip. That is definitely a lot better.

@AVaksman AVaksman changed the title fix: Delete env var after test if it was not set fix: delete env var after test if it was not set Dec 31, 2019
Copy link
Contributor

@hengfengli hengfengli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@olavloite olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 4, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 4, 2020
@olavloite olavloite merged commit 7a1f40d into googleapis:master Jan 4, 2020
@olavloite olavloite deleted the delete-env-var-after-test branch January 4, 2020 13:51
AVaksman pushed a commit to AVaksman/nodejs-spanner that referenced this pull request Jan 21, 2020
* fix: delete env var after test if it was not set

* fix: wrap tests in suite

Co-authored-by: Benjamin E. Coe <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants