SamVerschueren / alfy-test

Test your Alfy workflows
MIT License
21 stars 8 forks source link

Access `cache` and `conf` #1

Closed SamVerschueren closed 8 years ago

SamVerschueren commented 8 years ago

Currently, using alfy-test is pretty straightforward and easy to use. But it has some shortcomings. It would be nice if you could get access to the alfy cache and config.

As I see it, we have two options to implement this.

Return an object

Rollback the fact that we return the items directly instead of the result object and attach more values to it like cache, config.

import test from 'ava';
import alfyTest from 'alfy-test';

test(() => {
    const result = await alfyTest('workflow input');

    console.log(result);
    //=> {items: [{title: 'foo'}], cache: [CacheConf], conf: [Conf]}
});

Add preconfiguration

Sorry about the title :), let's just provide an example.

import test from 'ava';
import alfyTest from 'alfy-test';

test.beforeEach(t => {
    t.context.alfyTest = alfyTest({
        // environment variables goes here
    });
});

test(t => {
    const alfyTest = t.context.alfyTest;

    const result = await alfyTest('workflow input');

    console.log(result);
    //=> [{title: 'foo'}]

    console.log(alfyTest.cache)
    //=> [CacheConf]
});

Downside is that this is a little bit more work but might be more flexible.

// @sindresorhus

sindresorhus commented 8 years ago

How about:

import test from 'ava';
import alfyTest from 'alfy-test';

test(t => {
    const result = await alfyTest('workflow input', {
        // options and environment variables goes here
    });

    console.log(result);
    //=> [{title: 'foo'}]

    console.log(alfyTest.cache)
    //=> [CacheConf]
});
SamVerschueren commented 8 years ago

That would be the simplest the solution. The downside however is that it wouldn't allow you to run your tests concurrently.

At the moment, every time alfyTest() is called, a new cache and data directory is created with tempfile.

To keep being able to run tests concurrently, I thought about the preconfiguration solution as described above.

Not sure If I explained the issue well enough. If not, feel free to say so and I'll try to rephrase :p.

sindresorhus commented 8 years ago

At the moment, every time alfyTest() is called, a new cache and data directory is created with tempfile.

Isn't this what makes it possible to run the tests concurrently? A new temp dir is used for each test.

SamVerschueren commented 8 years ago

Yes, that's my point :). But with your example, where does alfyTest.cache points to when two tests run at the same time. They will probably point to the same cache which is not correct.

  1. Start test 1 - Create cache at ~/tmp/foo
  2. Start test 2 - Create cache at ~/tmp/bar
  3. Access cache in test 1, it will point to ~/tmp/bar which is incorrect
sindresorhus commented 8 years ago

Ah, I see my mistake now. Let's go with the instance then.