onepub-dev / dcli

An extensive library and tooling for building console/cli applications and scripts using the Dart programming language.
245 stars 26 forks source link

Allow reset of Env.mock #184

Closed passsy closed 2 years ago

passsy commented 2 years ago

Allows

    Env.mock = Env();
    addTearDown(() {
      Env.mock = null;
    });
bsutton commented 2 years ago

Thanks for the patch.

In the latest release I've actually removed this method. I've moved to using a new method withEnvironment which utilises a new package I've released Scope. withEnvironment allows us to set a scoped environment change which doesn't affect code outside of the scope.

   withTestScope((outerTempDir) {
        withEnvironment(() {
          /// create a pub-cache using the test scope's HOME
          Scope()
            ..value(PubCache.scopeKey, PubCache.forScope())
            ..run(() {
              if (Settings().isWindows) {
                expect(
                    PubCache().pathToBin,
                    equals(
                        join(outerTempDir, 'test_cache', '.pub_cache', 'bin')));
              } else {
                expect(
                  PubCache().pathToBin,
                  equals(join(outerTempDir, 'test_cache', '.pub_cache', 'bin')),
                );
              }
            });
        }, environment: {
          'PUB_CACHE': join(outerTempDir, 'test_cache', '.pub_cache')
        });
      });
passsy commented 2 years ago

That's even better because it doesn't share state between tests 👍