# Chefspec execute resource with cwd

**URL:** <https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970>\
**Category:** Chef Automate\
**Created:** [April 26, 2018, 10:25am UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970 "2018-04-26T10:25:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian-kurt](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brian-kurt](https://discourse.chef.io/u/Brian-kurt)\
**Post date:** [April 26, 2018, 10:25am UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/1 "2018-04-26T10:25:28Z")

</div>

HI, i am trying to write tests for my recipe that has the “execute” resource,which specifies the “cwd” and has “command”. i was wondering if it’s possible to do a test/check on the “cwd” part.

thanks,

Brian Phalane

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [April 26, 2018, 11:11am UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/2 "2018-04-26T11:11:56Z")

</div>

Hi,

You can check any property of a resource with with (not a typo 🙂 ). The checspec docs have this [1]

```auto
require 'chefspec'

describe 'example::default' do
  let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

  it 'adds the member vagrant to the docker group' do
    expect(chef_run).to modify_group('docker').with(members: ['vagrant'])
  end
end

```

[1] [https://github.com/chefspec/chefspec#making-assertions](https://github.com/chefspec/chefspec#making-assertions)

---

<div class="post-metadata">

**Author:** ![Brian-kurt](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brian-kurt](https://discourse.chef.io/u/Brian-kurt)\
**Post date:** [April 26, 2018, 11:39am UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/3 "2018-04-26T11:39:19Z")

</div>

This is how i wrote it so far

recipe

execute ‘clean directory’ do  
cwd ‘/my/current/working/directory’  
command ‘rm -f _.gz_’  
end

spec

it ‘clean directory’ do  
expect(chef\_run).to run\_execute(‘rm -f\*.gz\*’)  
end

my problem with this is, if someone modify the cwd “/my/current/working/directory” , tests will still pass. which is not correct.

My question is, is it possible to stub it, or allow with cwd to be checked/tested?

Thanks

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [April 26, 2018, 11:56am UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/4 "2018-04-26T11:56:27Z")

</div>

Yes, it is possible. Please read my answer and read the Documentation in the link I posted.  
And for future topics, please use the formatting provided by this forum for better readability.

---

<div class="post-metadata">

**Author:** ![Brian-kurt](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brian-kurt](https://discourse.chef.io/u/Brian-kurt)\
**Post date:** [May 3, 2018, 4:53pm UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/5 "2018-05-03T16:53:15Z")

</div>

Hi @joerg.herzinger,

I still haven’t figure this out. do you mind helping? all cases i have tried, gives me error.

Thanks,

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [May 3, 2018, 7:06pm UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/6 "2018-05-03T19:06:14Z")

</div>

> [@Brian-kurt](#):
>
> it ‘clean directory’ do  
> expect(chef\_run).to run\_execute(‘rm -f\*.gz\*’)  
> end

Ok, so two things:  
1: You have to test against the resource and identify them by their name. Your execute is called "clean directory" while you try to find the command.  
2: You can test the properties with "with" passing a hash.

So in end you need something like this `expect(chef_run).to run_execute('clean directory').with( command: 'rm -f *.gz', cwd: '/dir/ecto/ry')`

---

<div class="post-metadata">

**Author:** ![Brian-kurt](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brian-kurt](https://discourse.chef.io/u/Brian-kurt)\
**Post date:** [May 3, 2018, 9:33pm UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/7 "2018-05-03T21:33:43Z")

</div>

This is the same i have been trying, however i did not succeed

1. INT3\_Backend::default When all attributes are default, on Ubuntu 16.04 clean dir  
Failure/Error: expect(chef\_run).to run\_execute (‘clean dir’).with(command: ‘rm -f _.gz_’,cwd: ‘/export/home/devopadm’)

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [May 3, 2018, 9:48pm UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/8 "2018-05-03T21:48:20Z")

</div>

You put a space between `run_execute` and `('clean dir')`. That is not allowed.

---

<div class="post-metadata">

**Author:** ![Brian-kurt](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brian-kurt](https://discourse.chef.io/u/Brian-kurt)\
**Post date:** [May 4, 2018, 11:47am UTC](https://discourse.chef.io/t/chefspec-execute-resource-with-cwd/12970/9 "2018-05-04T11:47:13Z")

</div>

After removing the whitespace it worked. 😀

Thanks guys
