 connect = require 'superexec/lib/connect'
 misc = require '../lib/misc'
 
+they = (msg, callback) ->
+  it "#{msg} (local)", (next) ->
+    callback null, next
+  it "#{msg} (remote)", (next) ->
+    connect host: 'localhost', (err, ssh) ->
+      callback ssh, next
+
 describe 'write', ->
 
   scratch = test.scratch @
   
-  it 'should write a file', (next) ->
-    mecano.write
+  they 'should write a file', (ssh, next) ->
+    # Write the content
+    mecano.write
+      ssh: ssh
       content: 'Hello'
       destination: "#{scratch}/file"
     , (err, written) ->
       return next err if err
-      written.should.eql 1
-      fs.readFile "#{scratch}/file", 'ascii', (err, content) ->
-        content.should.eql 'Hello'
-        next()
-  
-  it 'create a backup', (next) ->
-    # First we create a file
-    mecano.write
+      # File has been created
+      written.should.eql 1
+      # Write the same content
+      mecano.write
+        ssh: ssh
+        content: 'Hello'
+        destination: "#{scratch}/file"
+      , (err, written) ->
+        return next err if err
+        # Content has change
+        written.should.eql 0
+        misc.file.readFile ssh, "#{scratch}/file", (err, content) ->
+          content.should.eql 'Hello'
+          next()
+  
+  they 'doesnt increment if destination is same than generated content', (ssh, next) ->
+    mecano.write
+      ssh: ssh
       content: 'Hello'
       destination: "#{scratch}/file"
-      backup: true
-    , (err, written) ->
-      return next err if err
-      # If nothing has change, there should be no backup
-      mecano.write
+    , (err, written) ->
+      return next err if err
+      written.should.eql 1
+      mecano.write
+        ssh: ssh
         content: 'Hello'
         destination: "#{scratch}/file"
-        backup: '.bck'
       , (err, written) ->
         return next err if err
         written.should.eql 0
-        misc.file.exists null, "#{scratch}/file.bck", (err, exists) ->
-          exists.should.be.false
-          # If content is different, check the backup
-          mecano.write
-            content: 'Hello Node'
-            destination: "#{scratch}/file"
-            backup: '.bck'
-          , (err, written) ->
-            return next err if err
-            fs.readFile "#{scratch}/file.bck", 'ascii', (err, content) ->
-              content.should.eql 'Hello Node'
-              next()
-  
-  it 'doesnt increment if destination is same than generated content', (next) ->
-    mecano.write
+        next()
+  
+  they 'doesnt increment if destination is same than generated content', (ssh, next) ->
+    mecano.write
+      ssh: ssh
       content: 'Hello'
       destination: "#{scratch}/file"
     , (err, written) ->
       return next err if err
-      written.should.eql 1
-      mecano.write
-        content: 'Hello'
-        destination: "#{scratch}/file"
-      , (err, written) ->
-        return next err if err
-        written.should.eql 0
-        next()
-  
-  it 'doesnt increment if destination is same than generated content', (next) ->
-    mecano.write
-      content: 'Hello'
-      destination: "#{scratch}/file"
-    , (err, written) ->
-      return next err if err
-      mecano.write
+      mecano.write
+        ssh: ssh
         source: "#{scratch}/file"
         destination: "#{scratch}/file_copy"
       , (err, written) ->
         return next err if err
         written.should.eql 1
-        fs.readFile "#{scratch}/file", 'ascii', (err, content) ->
+        misc.file.readFile ssh, "#{scratch}/file", (err, content) ->
           content.should.eql 'Hello'
           next()
   
-  it 'empty file', (next) ->
-    mecano.write
+  they 'empty file', (ssh, next) ->
+    mecano.write
+      ssh: ssh
       content: ''
       destination: "#{scratch}/empty_file"
     , (err, written) ->
       return next err if err
       written.should.eql 1
-      misc.file.readFile null, "#{scratch}/empty_file", (err, content) ->
+      misc.file.readFile ssh, "#{scratch}/empty_file", (err, content) ->
         return next err if err
         content.should.eql ''
         next()
   
-  it 'with from and with to', (next) ->
-    mecano.write
+  they 'with from and with to', (ssh, next) ->
+    mecano.write
+      ssh: ssh
       from: '# from\n'
       to: '# to'
       content: 'here we are\n# from\nlets try to replace that one\n# to\nyou coquin'

            return next err if err
            content.should.eql 'here we are\nyou coquin\nAdd this line'
            next()
    it 'will detect new line if no match (local)', (next) ->
      willDetectNewLineIfNoMatch null, next
    it 'will detect new line if no match (remote)', (next) ->
      connect host: 'localhost', (err, ssh) ->
        willDetectNewLineIfNoMatch ssh, next

    it 'create file if not exists', (next) ->
      # File does not exist, it create it with the content
      mecano.write
        match: /will never be found/
        destination: "#{scratch}/file"
        replace: 'Add this line'

          return next err if err
          written.should.eql 1
          # Check file content
          misc.file.readFile ssh, "#{scratch}/file", (err, content) ->
            return next err if err
            content.should.eql 'here we are\nyou coquin\nAdd this line'
            next()

    they 'will detect new line if no match', (ssh, next) ->
      # Create file for the test
      mecano.write
        ssh: ssh
